向 WPF 自定义控件添加触发器

发布于 2024-08-23 05:30:49 字数 3097 浏览 2 评论 0原文

我有一个名为 DatePicker.xaml 的自定义用户控件。其背后的代码是 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 和 DayOfWeek 属性会发生变化。

我在页面 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-compatibility/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>

然后我想要的是,当用户单击自定义用户控件(日历)中的一天时,我想处理日历中的 DayOfWeek 属性更改并在 PageHoras.xaml 中触发触发器所以我想根据所选的星期几启用/禁用 PageHoras.xaml 中的复选框(星期六 -> 启用,一周中的其余日子 -> 禁用)。

我该怎么做?

是否可以在 PageHoras.xaml 中为此自定义用户控件添加触发器?像这样:

<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>

或这样:

<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>

但 XAML 解析器告诉我这是不正确的。如果可能的话,我想用 XAML 代码来完成。

非常感谢,

托尼。

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-compatibility/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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈独秀 2024-08-30 05:30:49

Toni,

您是否尝试过使用 WPF 中提供的 DatePicker 或 Calendar 控件?听起来您只想将复选框状态绑定到所选日历日期中的更改,但您希望在 XAML 而不是代码中执行此操作。

您想要做的是在当天为 5 或 DayOfWeek 为星期六时选中复选框(这些是类似的语句)吗?我对这个问题的理解正确吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文