2012-03-22 105 views
2

我已经查看了一些已经在stackoverflow上的答案,但没有一个答案似乎能够给我我需要的东西。问题似乎很密切地描述我的情况,但解决方案并不适合我。当WPF中的值发生更改时更新行颜色/xaml

我有一个DataGrid中的复选框列,并且当用户单击复选框时,由于数据绑定到BindableList,与它关联的值(IsOnList)被更新。可绑定列表的类型对象还存储一个布尔值来指示该值是否已更改(更改),当IsOnList更改时该值将被翻转。

如果数值已更改,我想通过将背景设置为红色来向用户指出此情况。下面是xaml的相关部分。我已经尝试了数据网格行上的setter和触发器样式,但运气不大(为了测试,我也将鼠标放在了上面,以确保它们被拾取,并且它运行良好,并尝试像TwoWay这样的事情)。数据触发器将在网格加载时调用,因此,当网格加载时,更改始终为假,背景将为蓝色。当我单击复选框时,该更新更改为true,背景不会更改颜色。我甚至为Changed属性添加了一列,以查看该值是否更改,但不是。

<UserControl.Resources> 
    <vm:ProductListEditViewModel x:Key="ViewModel"/> 
    <vm:ChangedHighlightConverter x:Key="ChangedHighlightConverter"/> 
</UserControl.Resources> 

<tk:DataGrid ItemsSource="{Binding EditableLists}" x:Name="dataGrid" Grid.Row="1" 
       ... 
       SelectedItem="{Binding Path=SelectedAttribute, Mode=TwoWay}"> 

     <tk:DataGrid.RowStyle> 
      <Style TargetType="tk:DataGridRow"> 
       <Setter Property="Background" Value="{Binding Changed, Converter={StaticResource ChangedHighlightConverter}}" /> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding Path=Changed, Mode=OneWay}" Value="True"> 
         <Setter Property="Background" Value="Red" /> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding Changed, Mode=TwoWay}" Value="True"> 
         <Setter Property="Background" Value="Red" /> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding Changed}" Value="False"> 
         <Setter Property="Background" Value="Blue" /> 
        </DataTrigger> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter Property="Foreground" Value="Red"/> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </tk:DataGrid.RowStyle> 

     <tk:DataGrid.Columns> 
      <tk:DataGridTextColumn Header="Group Name" Binding="{Binding GroupDescription}" Width="120" IsReadOnly="True"/> 
      <tk:DataGridTextColumn Header="List Name" Binding="{Binding ListDescription}" Width="120" IsReadOnly="True"/> 
      <tk:DataGridTextColumn Header="List ID" Binding="{Binding ListId}" Width="50" IsReadOnly="True"/> 
      <tk:DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" Width="50" IsReadOnly="True"/> 
      <tk:DataGridTextColumn Header="Comment" Binding="{Binding Description}" Width="120" IsReadOnly="True"/> 
      <tk:DataGridTextColumn Header="Changed" Binding="{Binding Changed, Mode=TwoWay}" Width="40" IsReadOnly="True"/> 
      <tk:DataGridTemplateColumn Header="On List" Width="50"> 
       <tk:DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <CheckBox HorizontalAlignment="Center" IsChecked="{Binding Path=IsOnList, UpdateSourceTrigger=PropertyChanged}"/> 
        </DataTemplate> 
       </tk:DataGridTemplateColumn.CellTemplate> 
      </tk:DataGridTemplateColumn> 
     </tk:DataGrid.Columns> 

    </tk:DataGrid> 

我的类的相关部分:

public bool IsOnList 
    { 
     get { return isOnList; } 
     set { 
      Changed = !changed; 
      isOnList = value; 
     } 
    } 

public bool Changed 
{ 
     get { return changed; } 
     set { this.changed = value; } 
} 

我很新的C#和XAML,所以一直在努力试图使这项工作。我错过了明显的东西吗?

回答

2

没有看到你的类的整个代码,你可能需要在你的类中实现INotifyPropertyChanged,以便WPF绑定知道属性何时发生变化。您没有发射任何PropertyChanged事件,因此背景样式不会更新(也不会Changed属性DataGridRow)。

+0

谢谢,会给你一个去,让你知道它是如何去。 – Sokratees9 2012-03-22 16:46:07

+0

工作,谢谢。 – Sokratees9 2012-03-22 16:56:21

相关问题