简体   繁体   中英

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

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

My question:

How can I rewrite the DataTrigger so that it is setting CsDataTemplate.Description instead of directly editing DescField 's TextProperty .

You have to add INotifyPropertyChanged to your setters in order for your DataTriggers to work. Your options are:

1) Write a ViewModel and duplicate each field, raising the property change notification as each is set.

2) Use Castle DynamicProxy to create a wrapper for your models that does this automatically.

3) Use Fody, which will add it to your model class IL with a post-processing step at compile-time.

You have to do 2 things :

  1. You need to either implement INotifyPropertyChanged interface or make your bindable object a DependencyObject by inheriting from DependencyObject class.

  2. In your binding, use <TextBlock Text="{Binding Path=Desc, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ... />

UpdateSourceTrigger is the main culprit which is needed with DataGrid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM