Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过绑定动态更改DataTemplate样式_C#_Wpf_Binding_Datatemplate - Fatal编程技术网

C# 通过绑定动态更改DataTemplate样式

C# 通过绑定动态更改DataTemplate样式,c#,wpf,binding,datatemplate,C#,Wpf,Binding,Datatemplate,我有一个带有两个子DataTemplate的父DataTemplate,并希望根据触发器仅更改子模板的一个子组件的属性,例如Label.Foreground、TextBox.Background等。我遇到了一个建议使用相对资源绑定的方法,但经过多次尝试后,我无法让它工作,而且我不确定如何只影响其中一个子模板。下面显示了我的预期最终结果任何/所有右子属性都需要更改,以显示它们与左子属性不同 父数据模板xaml: 夫妻财产是 public ObservableCollection<Couple

我有一个带有两个子DataTemplate的父DataTemplate,并希望根据触发器仅更改子模板的一个子组件的属性,例如Label.Foreground、TextBox.Background等。我遇到了一个建议使用相对资源绑定的方法,但经过多次尝试后,我无法让它工作,而且我不确定如何只影响其中一个子模板。下面显示了我的预期最终结果任何/所有右子属性都需要更改,以显示它们与左子属性不同

父数据模板xaml:

夫妻财产是

public ObservableCollection<Couple> Couples { get; private set; }
我尝试过各种不同的触发器绑定,但都没有用。我想做的事可能吗?如果是,我错过了什么

每个请求的PersonDataTemplate
嗯,有人给出了答案,但出于某种原因将其删除。它给了我绑定到前台属性的想法。他们曾建议在CoupleDataTemplate中使用ContentControl.Tag属性,但这对多个子组件属性并不起作用

从删除的答案演变而来的解决方案是使用DataContext.IsFirstDifferent属性的相对绑定,并使用将布尔值转换为颜色。因此,对于PersonDataTemplate中的每个标签,我创建了一个自定义的相对绑定。例如:

<Label Grid.Row="0" Grid.Column="0"
       HorizontalAlignment="Right"
       Name="FirstLabel"
       Content="First:"
       Foreground="{Binding Path=DataContext.IsFirstDifferent,
            RelativeSource={RelativeSource
            Mode=FindAncestor, AncestorType={x:Type ContentControl}},
            Converter={StaticResource IsDifferentConverter}}"
       />

添加PersonDataTemplate的源,并且哪种类型是{Binding Couples}?如果是IList,那么绑定DataContext.IsFirstDifferent就没有意义。
public ObservableCollection<Couple> Couples { get; private set; }
<DataTemplate x:Key="PersonDataTemplate">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <Label Grid.Row="0" Grid.Column="0"
               HorizontalAlignment="Right"
               Name="FirstLabel"
               Content="First:"
               />
        <TextBox Grid.Row="0" Grid.Column="1"
                 HorizontalAlignment="Stretch"
                 Name="FirstTextBox"
                 Text="{Binding First}"
                 />
        <Label Grid.Row="1" Grid.Column="0"
               HorizontalAlignment="Right"
               Name="LastLabel"
               Content="Last:"
               />
        <TextBox Grid.Row="1" Grid.Column="1"
                 HorizontalAlignment="Stretch"
                 Name="LastTextBox"
                 Text="{Binding Last}"
                 />
        <Label Grid.Row="2" Grid.Column="0"
               HorizontalAlignment="Right"
               Name="SexLabel"
               Content="Sex:"
               />
        <ComboBox Grid.Row="2" Grid.Column="1"
                  HorizontalAlignment="Stretch"
                  Name="Sex"
                  ItemsSource="{Binding SexTypes, Mode=OneWay}"
                  SelectedItem="{Binding Sex}"
                  />
    </Grid>
</DataTemplate>
<Label Grid.Row="0" Grid.Column="0"
       HorizontalAlignment="Right"
       Name="FirstLabel"
       Content="First:"
       Foreground="{Binding Path=DataContext.IsFirstDifferent,
            RelativeSource={RelativeSource
            Mode=FindAncestor, AncestorType={x:Type ContentControl}},
            Converter={StaticResource IsDifferentConverter}}"
       />
public class DifferenceConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
    {
        return ((Boolean)value) ? "Red" : "Black";
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }

}