我有一個名爲DatePicker.xaml的自定義用戶控件。它的代碼背後,是DatePicker.xaml.cs:將觸發器添加到WPF自定義控件中
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;
}
}
<...>
}
}
日,當用戶點擊了一天標籤(當月的每一天都是在日曆標籤)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代碼來做。
非常感謝,
託尼。