值更改时,不会触发Datagrid数据触发器

时间:2015-11-05 14:56:57

标签: c# wpf data-binding datagrid datatrigger

我有一个dataGridwith列,它们绑定到数据表中的某些列

<DataGrid x:Name="WatchersGrid" ItemsSource="{Binding GroupWatchers}" AutoGenerateColumns="False" SelectedItem="{Binding SelectedItem}" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding  Path=[Name]}"/>
                        <DataGridTextColumn Header="Value" Binding="{Binding Path=[Value]}">
                            <DataGridTextColumn.ElementStyle>
                                <Style>
                                    <Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Header="InRange" Binding="{Binding [IsInRange]}"/>
            </DataGrid.Columns>
                    <DataGrid.RowStyle>
                        <Style TargetType="DataGridRow">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding [IsInRange]}" Value="False">
                                    <Setter Property="Foreground" Value="Crimson"></Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </DataGrid.RowStyle>
        </DataGrid>

我在“IsInRange”列上有一个数据触发器,如果​​值为false,则将前景属性设置为红色。 数据网格列内的值发生更改但颜色属性剂量会发生变化。 我试过NotifyPropertyChanged(“[IsInRange]”);当我的价值在变化,但没有帮助 任何想法?

编辑: 我的ViewModel

public class WatcherControlViewModel : INotifyPropertyChanged
{
  private DataTable _dt;
  private string _groupName;
  public DataRow[] GroupWatchers{get {return _dt.Select("Group = '" + GroupName + "'");}}
  public WatcherControlViewModel(DataTable dt, string name)
    {
        _dt = dt;
        _groupName = name;
       _dt.RowChanged += DataTableChangedEvent;
    }

    private void DataTableChangedEvent(object sender, DataRowChangeEventArgs e)
    {
        NotifyPropertyChanged("GroupWatchers");
        NotifyPropertyChanged("IsInRange");
    }
}

感谢

2 个答案:

答案 0 :(得分:0)

你试过没有方括号吗?您的模型是否实现了INotifyPropertyChanged接口? 如果您在该视图后面发布代码,可能会有所帮助。

答案 1 :(得分:0)

这取决于您的ViewModel以及您如何绑定它。从你的XAML暗示,你的ViewModel应该是这样的:

public class MyViewModel
{
    public DataTable WatchersGrid { get; set; }

    public MyViewModel()
    {
        WatchersGrid = new MyDataTable();
    }
}

你的装订应该是这样的:

public partial class Window1 : Window
{
    private MyViewModel vm;

    public Window1()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        vm = new MyViewModel();
        DataContext = vm;
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        vm.WatchersGrid.Rows[0]["IsInRange"] = !(bool)vm.WatchersGrid.Rows[0]["IsInRange"];
    }
}

注意测试按钮。出于测试目的,它仅为第一行在IsInRangetrue之间切换false值。

结果:

enter image description here enter image description here