开发者

Adding a trigger to a WPF custom control

I have a custom user control named DatePicker.xaml. Its code behind is DatePicker.xaml.cs:

namespace GesHoras.UserControls
{
/// <summary>
/// Lógica de interacción para DatePicker.xaml
/// </summary>
/// 
public partial class DatePicker : UserControl
{

    <...>

    private int _day;
    private int _year;
    private int _month;


    public int Day
    {
        get { return _day; }
        set 
        { 
            //DateTime dt;
            int _daysInMonth;

            _daysInMonth = System.DateTime.DaysInMonth(_year, _month);                
            if ((_day >= MIN_DAY) && (_day <= _daysInMonth))
            {
                _day = value;
            }
        }
    }

    public DayOfWeek DayOfWeek
    {
        get
        {
            DateTime dt = new DateTime(_year, _month, _day);
            return dt.DayOfWeek;
        }
    }
    <...>
}

}

Day and DayOfWeek properties changes when user clicks a day label (each day of the current month is a label in the calendar).

I use this custom control in a page PageHoras.xaml:

  <Page x:Class="GesHoras.Pages.PageHoras"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"       
  xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;
                                  assembly=PresentationFramework.Aero"        
  xmlns:local="clr-namespace:GesHoras.UserControls"
  xmlns:Classes="clr-namespace:GesHoras.Classes"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2006" 
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibi开发者_JAVA百科lity/2006"      
  Title="Horas" 
  Loaded="Page_Loaded">

  <...>

  <CheckBox Grid.Row="1" 
            Name="ckCompensar"
            Margin="10,0,10,5"
            HorizontalAlignment="Left"
            Width="{Binding Path=ActualWidth-10, ElementName=GrpHorario}">
            Compensar
  </CheckBox>

  <local:DatePicker x:Name="_Calendar">

  <...>
  </Page>

Then what I want is that when user clicks a day in the custom user control (calendar) I would like to handle DayOfWeek property change in the calendar and fire a trigger in PageHoras.xaml so I want to enable/disable a checkbox in PageHoras.xaml depends on the day of week selected (saturdary -> enabled, rest of days of the week -> disabled).

How can I do this?

Is it possible to add a trigger in PageHoras.xaml for this custom user control? something like this:

<local:DatePicker x:Name="_Calendar">
  <Trigger Property="DayOfWeek" Value="Saturday">
    <Setter Property="IsEnabled" TargetName="ckCompensar" Value="true"/>        
  </Trigger>

HERE I HAVE ANOTHER PROBLEM: HOW TO SET PROPERTY ISENABLED TO FALSE FOR TARGET   
ckCompensar WHEN DAY OF WEEK IS DIFFERENT FROM SATURDAY¿?

</local:DatePicker>

or this:

<local:DatePicker x:Name="_Calendar">
  <Trigger Property="Day" Value="5">
    <Setter Property="IsEnabled" TargetName="ckCompensar" Value="true"/>
  </Trigger>

HERE I HAVE THE SAME PROBLEM AGAIN: HOW TO SET PROPERTY ISENABLED TO FALSE FOR 
TARGET ckCompensar WHEN DAY OF WEEK IS DIFFERENT FROM 5¿?

</local:DatePicker>

But XAML parser tells me it is incorrect. I would like to do it with XAML code if it is possible.

Thanks very much,

Toni.


Toni,

Have you tried using the DatePicker or Calendar control available in WPF? It sounds like you just want to bind the checkbox state to changes in the Calendar date picked, but you want to do this in XAML and not in code.

And what you want to do is check the checkbox when the day is 5 or the DayOfWeek is Saturday (are these analogous statements)? Am I understanding the question correctly?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜