DataTrigger 在选中 RadioButton 时更新 DatePicker
DataTrigger to Update a DatePicker when a RadioButton IsChecked
我只是想根据单选按钮的 IsChecked 在 DatePicker 中设置 SelectedDate。当单选按钮 IsChecked 为真时,应在 DatePicker 中输入今天的日期。如果 IsChecked 为假,则 DatePicker 应为空。我已经尝试了数据触发器的各种再现,但 none 已经产生了任何好处。无论单选按钮是什么,DatePicker 中都不会填充任何内容。我缺少什么才能让它正常工作?
<DatePicker Grid.Row="19" Grid.Column="1" Width="120" SelectedDate="{Binding SelectedApplication.DateHireDeclineSent}"
Visibility="{Binding IsChecked, ElementName=HireDeclinedYes, Converter={StaticResource BooleanToVisibilityConverter}}" >
<DatePicker.Style>
<Style TargetType="DatePicker">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=HireDeclinedYes, Path=IsChecked, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Value="True">
<Setter Property="SelectedDate" Value="{x:Static sys:DateTime.Now}"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=HireDeclinedYes, Path=IsChecked}" Value="False">
<Setter Property="SelectedDate" Value="{x:Null}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DatePicker.Style>
</DatePicker>
<StackPanel Grid.Row="16" Grid.Column="4" Orientation="Horizontal"
Visibility="{Binding IsChecked,ElementName=NoHireAdd, Converter={StaticResource BooleanToVisibilityConverter}}">
<RadioButton x:Name="HireDeclinedYes" Content="Yes" IsChecked="{Binding SelectedApplication.HireDeclined}" />
<RadioButton x:Name="HireDeclinedNo" Margin="5,0,0,0" Content="No" IsChecked="{Binding SelectedApplication.HireDeclined, Converter={StaticResource BoolInverterConverter}}" />
</StackPanel>
由于 DatePicker SelectedDate 绑定到 SelectedApplication.DateHireDeclineSent
并且单选按钮 IsChecked 属性 绑定到SelectedApplication.HireDeclined
,您可以在 SelectedApplication.HireDeclined 的 setter 中也更改 SelectedApplication.DateHireDeclineSent 属性 并提高 属性 更改事件?
这将更改绑定到 DatePicker 的 属性,属性 changed 事件将通知视图有关更改,从而更改 DatePicker SelectedDate。
就像这样:
private bool _hireDeclined;
public bool HireDeclined
{
get => _hireDeclined;
set
{
_hireDeclined = value;
NotifyPropertyChanged("HireDeclined");
if (//some condition)
DateHireDeclineSent = DateTime.Now;
}
}
private DateTime _dateHireDeclineSent;
public DateTime DateHireDeclineSent
{
get => _dateHireDeclineSent;
set
{
_dateHireDeclineSent = value;
NotifyPropertyChanged("DateHireDeclineSent");
}
}
如果您删除 SelectedDate="{Binding SelectedApplication.DateHireDeclineSent}"
并使用 setter:
设置默认值,它应该可以工作
<DatePicker Grid.Row="19" Grid.Column="1" Width="120"
Visibility="{Binding IsChecked, ElementName=HireDeclinedYes, Converter={StaticResource BooleanToVisibilityConverter}}" >
<DatePicker.Style>
<Style TargetType="DatePicker">
<Setter Property="SelectedDate" Value="{Binding SelectedApplication.DateHireDeclineSent}" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=HireDeclinedYes, Path=IsChecked, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Value="True">
<Setter Property="SelectedDate" Value="{x:Static sys:DateTime.Now}"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=HireDeclinedYes, Path=IsChecked}" Value="False">
<Setter Property="SelectedDate" Value="{x:Null}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DatePicker.Style>
</DatePicker>
本地值优先于样式 setters:https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-property-value-precedence
我只是想根据单选按钮的 IsChecked 在 DatePicker 中设置 SelectedDate。当单选按钮 IsChecked 为真时,应在 DatePicker 中输入今天的日期。如果 IsChecked 为假,则 DatePicker 应为空。我已经尝试了数据触发器的各种再现,但 none 已经产生了任何好处。无论单选按钮是什么,DatePicker 中都不会填充任何内容。我缺少什么才能让它正常工作?
<DatePicker Grid.Row="19" Grid.Column="1" Width="120" SelectedDate="{Binding SelectedApplication.DateHireDeclineSent}"
Visibility="{Binding IsChecked, ElementName=HireDeclinedYes, Converter={StaticResource BooleanToVisibilityConverter}}" >
<DatePicker.Style>
<Style TargetType="DatePicker">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=HireDeclinedYes, Path=IsChecked, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Value="True">
<Setter Property="SelectedDate" Value="{x:Static sys:DateTime.Now}"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=HireDeclinedYes, Path=IsChecked}" Value="False">
<Setter Property="SelectedDate" Value="{x:Null}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DatePicker.Style>
</DatePicker>
<StackPanel Grid.Row="16" Grid.Column="4" Orientation="Horizontal"
Visibility="{Binding IsChecked,ElementName=NoHireAdd, Converter={StaticResource BooleanToVisibilityConverter}}">
<RadioButton x:Name="HireDeclinedYes" Content="Yes" IsChecked="{Binding SelectedApplication.HireDeclined}" />
<RadioButton x:Name="HireDeclinedNo" Margin="5,0,0,0" Content="No" IsChecked="{Binding SelectedApplication.HireDeclined, Converter={StaticResource BoolInverterConverter}}" />
</StackPanel>
由于 DatePicker SelectedDate 绑定到 SelectedApplication.DateHireDeclineSent
并且单选按钮 IsChecked 属性 绑定到SelectedApplication.HireDeclined
,您可以在 SelectedApplication.HireDeclined 的 setter 中也更改 SelectedApplication.DateHireDeclineSent 属性 并提高 属性 更改事件?
这将更改绑定到 DatePicker 的 属性,属性 changed 事件将通知视图有关更改,从而更改 DatePicker SelectedDate。
就像这样:
private bool _hireDeclined;
public bool HireDeclined
{
get => _hireDeclined;
set
{
_hireDeclined = value;
NotifyPropertyChanged("HireDeclined");
if (//some condition)
DateHireDeclineSent = DateTime.Now;
}
}
private DateTime _dateHireDeclineSent;
public DateTime DateHireDeclineSent
{
get => _dateHireDeclineSent;
set
{
_dateHireDeclineSent = value;
NotifyPropertyChanged("DateHireDeclineSent");
}
}
如果您删除 SelectedDate="{Binding SelectedApplication.DateHireDeclineSent}"
并使用 setter:
<DatePicker Grid.Row="19" Grid.Column="1" Width="120"
Visibility="{Binding IsChecked, ElementName=HireDeclinedYes, Converter={StaticResource BooleanToVisibilityConverter}}" >
<DatePicker.Style>
<Style TargetType="DatePicker">
<Setter Property="SelectedDate" Value="{Binding SelectedApplication.DateHireDeclineSent}" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=HireDeclinedYes, Path=IsChecked, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Value="True">
<Setter Property="SelectedDate" Value="{x:Static sys:DateTime.Now}"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=HireDeclinedYes, Path=IsChecked}" Value="False">
<Setter Property="SelectedDate" Value="{x:Null}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DatePicker.Style>
</DatePicker>
本地值优先于样式 setters:https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-property-value-precedence