[英]DataTrigger not setting properties in poco
Hi guys I couldn't find a question that really helps me with what I am trying to do. 嗨,大家好,我找不到一个真正可以帮助我解决我问题的问题。
I have created a DataTemplate
to help with some visualization of my data. 我创建了一个DataTemplate
来帮助我的数据进行可视化。
<DataTemplate x:Key="CsChangeDataDataTemplate" DataType="database:CsChangeData">
<Grid Width="240">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="48"/>
<ColumnDefinition Width="192"/>
</Grid.ColumnDefinitions>
<Canvas x:Name="icon" Background="{Binding Path=Background, FallbackValue={StaticResource UNK}, TargetNullValue={StaticResource UNK}}" Height="48" Width="48"/>
<StackPanel Grid.Column="1">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Account" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5,5,5,2.5" Height="16" Width="64" />
<TextBlock Text="{Binding Account, FallbackValue=Unknown, TargetNullValue=Unknown}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="128" Margin="5,5,5,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Description" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5,2.5,5,2.5" Height="16" Width="64"/>
<TextBlock x:Name="DescField" Text="{Binding Description, FallbackValue=Unknown, TargetNullValue=Unknown}" HorizontalAlignment="Left" Width="128" VerticalAlignment="Top" Margin="5,2.5,5,0"/>
</StackPanel>
</StackPanel>
</Grid>
<DataTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=ChangeType}" Value="None" />
<Condition Binding="{Binding Path=Service}" Value="Trunking"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" TargetName="icon" Value="{StaticResource TR}"/>
<Setter Property="Text" TargetName="DescField" Value="Trunking"/>
</MultiDataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
I have also created the POCO for it 我也为此创建了POCO
[Serialized]
public class CsChangeData
{
[NonSerialized]
private DrawingBrush _background;
public DrawingBrush Background
{
get { return _background; }
set { _background = value; }
}
public string Account { get; set; }
public CsChangeType ChangeType { get; set; }
public CsServices Service { get; set; }
public string Description { get; set; }
public CsTicket Ticket { get; set; }
public override string ToString()
{
return $"{Description}\r\n" +
$"{Ticket.Account}";
}
}
This works fine for the visuals, but I just realize I am overriding the binding set in the DataTemplate
. 这对于视觉效果很好,但是我只是意识到我正在重写DataTemplate
的绑定集。
My question: 我的问题:
How can I rewrite the DataTrigger
so that it is setting CsDataTemplate.Description
instead of directly editing DescField
's TextProperty
. 如何重写DataTrigger
以便它设置CsDataTemplate.Description
而不是直接编辑DescField
的TextProperty
。
You have to add INotifyPropertyChanged to your setters in order for your DataTriggers to work. 您必须将INotifyPropertyChanged添加到设置器中,才能使DataTriggers正常工作。 Your options are: 您的选择是:
1) Write a ViewModel and duplicate each field, raising the property change notification as each is set. 1)编写一个ViewModel并复制每个字段,并在设置每个字段时引发属性更改通知。
2) Use Castle DynamicProxy to create a wrapper for your models that does this automatically. 2)使用Castle DynamicProxy为您的模型创建一个自动执行此操作的包装器。
3) Use Fody, which will add it to your model class IL with a post-processing step at compile-time. 3)使用Fody,它将在编译时通过后处理步骤将其添加到模型类IL中。
You have to do 2 things : 您必须做两件事:
You need to either implement INotifyPropertyChanged interface or make your bindable object a DependencyObject by inheriting from DependencyObject class. 您需要实现INotifyPropertyChanged接口,或者通过从DependencyObject类继承来使可绑定对象成为DependencyObject。
In your binding, use <TextBlock Text="{Binding Path=Desc, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ... />
在绑定中,使用<TextBlock Text="{Binding Path=Desc, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ... />
UpdateSourceTrigger is the main culprit which is needed with DataGrid. UpdateSourceTrigger是DataGrid所需的主要元凶。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.