无法解析datatrigger wpf中的绑定

时间:2016-12-16 09:12:26

标签: c# wpf xaml binding foreground

使用此代码:

  <DataGrid
    Grid.Row="1"
    ItemsSource="{Binding DdaOptions.FilteredRecords.FilteredRecords}"
    AutoGenerateColumns="False"
    VerticalScrollBarVisibility="Auto"
    GridLinesVisibility="None"
    Background="{StaticResource White1}"
    BorderThickness="1"
    BorderBrush="{StaticResource White1}"
    Style="{StaticResource Roboto10DataGrid}"
    AreRowDetailsFrozen="True"
    CanUserAddRows="False">
  <DataGrid.Columns>
    <DataGridTextColumn Header="GuideNumber" Binding="{Binding GuideNumber}" />
    <DataGridTextColumn Header="PartNumber" Binding="{Binding PartNumber}" />
    <DataGridTextColumn Header="Options" Binding="{Binding Options}" />
    <DataGridTextColumn Header="Description" Binding="{Binding Description}" />
    <DataGridTextColumn Header="PartStock" Binding="{Binding PartStock}" />
    <DataGridTextColumn Header="InterventionType" Binding="{Binding InterventionType}" />
  </DataGrid.Columns>
<DataGrid.RowStyle>
    <Style TargetType="DataGridRow" BasedOn="{StaticResource {x:Type DataGridRow}}">
      <Style.Triggers>
        <DataTrigger Binding="{Binding IsInDda}" Value="True">
          <Setter Property="Foreground" Value="{StaticResource DarkAccent1}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Highlighted}" Value="True">
          <Setter Property="Foreground">
            <Setter.Value>
             <SolidColorBrush Color="{Binding DdaOptions.MyColor}"/>
            </Setter.Value>
          </Setter>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </DataGrid.RowStyle>
</DataGrid>

绑定Color="{Binding DdaOptions.MyColor}"不起作用。如果我更换行

<SolidColorBrush Color="{Binding DdaOptions.MyColor}"/>

这一行

<SolidColorBrush Color="Red"/>

它有效。使用调试正确设置DdaOptions.MyColor属性。 如何更改代码以解决此问题? 我试过用转换器代替

<SolidColorBrush Color="{Binding DdaOptions.MyColor}"/>

但问题仍然存在。 MyColor属性的类型是Color。 Dda是ViewModel,它实现了INotifyPropertyChanged。 有什么想法吗?

更新

使用此代码

 <DataTrigger Binding="{Binding Highlighted}" Value="True">
   <Setter Property="Foreground" Value="{Binding DdaOptions.MyColor}"/>
 </DataTrigger>

而不是

  <DataTrigger Binding="{Binding Highlighted}" Value="True">
    <Setter Property="Foreground">
      <Setter.Value>
        <SolidColorBrush Color="{Binding DdaOptions.MyColor}"/>
      </Setter.Value>
    </Setter>
  </DataTrigger>

颜色总是变黑。 MyColor是一个Brush,我以这种方式设置它:

MyColor = new SolidColorBrush(Color.FromRgb(myColor.R, myColor.G, yColor.B));

2 个答案:

答案 0 :(得分:1)

如果DdaOptions.MyColor源属性返回 Brush ,则应直接将Foreground属性设置为此画笔:

<DataTrigger Binding="{Binding Highlighted}" Value="True">
    <Setter Property="Foreground" Value="{Binding DdaOptions.MyColor}" />
</DataTrigger>

答案 1 :(得分:1)

首先:

如果std::ofstream output("folder1\\"+p_strFilename); 的类型为DdaOptions.MyColor,请使用此项:

SolidColorBrush

或者如果是<DataTrigger Binding="{Binding Highlighted}" Value="True"> <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=DataContext.DdaOptions.MyColor}"/> </DataTrigger> 类型,那么:

Color

当然,您必须将<DataTrigger Binding="{Binding Highlighted}" Value="True"> <Setter Property="Foreground"> <Setter.Value> <SolidColorBrush Color="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=DataContext.DdaOptions.MyColor}"/> </Setter.Value> </Setter> </DataTrigger> 更改为正确的类型。