【问题标题】:Style WPF DataGrid Row on Binding Property绑定属性上的样式 WPF DataGrid 行
【发布时间】:2014-08-15 03:19:53
【问题描述】:

我有一个绑定到 EventRecords 列表的数据网格。 EventRecord 对象上有一个属性IsAutoEvent。我正在尝试根据此属性是否为真设置行上的背景颜色,但 DataContext 未按我的预期设置。

<DataGrid Name="EventGrid" IsReadOnly="True" AutoGenerateColumns="False" ItemsSource="{Binding Events}" SelectedItem="{Binding SelectedEvent}" CanUserAddRows="False" SelectionMode="Single" SelectionUnit="FullRow">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Start Time"  Width="Auto" Binding="{Binding StartTime, StringFormat={}{0:hh:mm:ss}}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>        
        <DataGridTextColumn Header="Description" Width="*" Binding="{Binding Description}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>
        <DataGridTextColumn Header="Comments" Width="*" Binding="{Binding Comment}"/>        
    </DataGrid.Columns>
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">            
            <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=IsAutoEvent}">
                        <Setter Property="Background" Value="Red"/>
                    </DataTrigger>
            </Style.Triggers>            
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

目前,这没有任何作用。我不认为数据上下文是正确的,因为在DataTrigger 下,{Binding} 显示视图的属性(例如 MainViewModel),而不是 DataGridRow(例如 EventRecord),正如我所期望的那样。

有什么想法吗?

【问题讨论】:

    标签: c# wpf xaml data-binding datagrid


    【解决方案1】:

    你还没有把Value放在DataTrigger

                   <DataTrigger Binding="{Binding Path=IsAutoEvent}" Value="True">
                        <Setter Property="Background" Value="Red"/>
                    </DataTrigger>
    

    【讨论】:

      【解决方案2】:

      请将Value 添加到您的DataTrigger

      <DataGrid Name="EventGrid" IsReadOnly="True" AutoGenerateColumns="False" ItemsSource="{Binding Events}" SelectedItem="{Binding SelectedEvent}" CanUserAddRows="False" SelectionMode="Single" SelectionUnit="FullRow">
          <DataGrid.Columns>
              <DataGridTextColumn Header="Start Time"  Width="Auto" Binding="{Binding StartTime, StringFormat={}{0:hh:mm:ss}}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>        
              <DataGridTextColumn Header="Description" Width="*" Binding="{Binding Description}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>
              <DataGridTextColumn Header="Comments" Width="*" Binding="{Binding Comment}"/>        
          </DataGrid.Columns>
          <DataGrid.RowStyle>
              <Style TargetType="{x:Type DataGridRow}">            
                  <Style.Triggers>
                      <DataTrigger Binding="{Binding Path=IsAutoEvent}" Value="True">
                          <Setter Property="Background" Value="Red"/>
                      </DataTrigger>
                  </Style.Triggers>            
              </Style>
          </DataGrid.RowStyle>
      </DataGrid>
      

      【讨论】:

      • 是的,完全正确。我根据提交的时间接受了@nit 的回答。但是无论如何都要为你 +1!
      猜你喜欢
      • 1970-01-01
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      • 2017-08-17
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      • 2011-05-31
      相关资源
      最近更新 更多