我试图弄清楚是否有可能让DataTemplate触发器更新非ui属性中的值。它似乎不起作用。见下文。当您单击以显示Person对象时,实例会在复选框关闭时显示Valid属性。
人物对象:
namespace WPFControls
{
public class Person : INotifyPropertyChanged
{
public string Name { get; set; }
private double _age = 0;
public double Age
{
get { return _age; }
set
{
if (_age != value)
{
_age = value;
OnPropertyChanged(nameof(Age));
}
else
{
Debug.WriteLine("NO CHANGE");
}
}
}
private bool valid = true;
public bool IsValid {
get { return valid; }
set
{
if (valid != value)
{
valid = value;
OnPropertyChanged(nameof(IsValid));
}
else
{
Debug.WriteLine("isvalud no change");
}
}
}
public override string ToString()
{
return $"{nameof(Name)}: {Name}, {nameof(Age)}: {Age} valid: {nameof(IsValid)} {IsValid}";
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
XAML:
<Window x:Class="WPFControls.AgeForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFControls"
mc:Ignorable="d"
Title="AgeForm" Height="300" Width="350">
<Window.Resources>
<DataTemplate DataType="{x:Type local:Person}">
<Grid Margin="10,10,10,10">
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0">Name:</TextBlock>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" Margin="8"/>
<TextBlock Name="AgeText" Grid.Row="1" Grid.Column="0">Age:</TextBlock>
<TextBox Name="AgeBox" Grid.Row="1" Grid.Column="1" Text="{Binding Age, UpdateSourceTrigger=PropertyChanged}" Margin="8"/>
<CheckBox Margin="50,0,0,0" Grid.Row="0" Grid.Column="0" Name="CheckBoxValid" IsChecked="{Binding IsValid, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Content="IS VALID?"/>
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Age}" Value="21">
<Setter TargetName="AgeText" Property="Background" Value="Red"/>
<Setter TargetName="CheckBoxValid" Property="CheckBox.IsChecked" Value="False"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Window.Resources>
<Grid Margin="20,20,20,20">
<Grid.RowDefinitions>
<RowDefinition Height="200"/>
<RowDefinition/>
</Grid.RowDefinitions>
<ContentControl Content="{Binding}"/>
<StackPanel Grid.Row="1">
<Button Name="ShowButton" Click="ShowButton_OnClick">_Show...</Button>
</StackPanel>
</Grid>
</Window>
请注意,Click事件只是MessageBox.Show(persion.ToString())
答案 0 :(得分:1)
Setter <Setter TargetName="CheckBoxValid" Property="CheckBox.IsChecked" Value="False"/>
删除现有绑定,而Person属性未更新。所以我建议删除该setter并在视图模型中添加验证逻辑:
public double Age
{
get { return _age; }
set
{
if (_age != value)
{
_age = value;
OnPropertyChanged(nameof(Age));
if (_age == 21) IsValid = false;
}
else
{
Debug.WriteLine("NO CHANGE");
}
}
}