WPF DataGrid: rows / columns / cells selected

I am embedding diff data in my project, and now I need to display the results for the user. (I check two arrays of arbitrary data and find inconsistencies in them, my results are something like: "Status: inconsistency, property: ... Index: ..." (some class)). So, now it works very well, at first I thought it would be easy to highlight the results in the DataGrid, but when I started implementing this, I realized that I just can’t imagine how to do it ... I need to select the cell and row presets .. Is there any general solution? PS DataGrid is tied to some data (using views). I don’t have much experience in WPF, so I don’t need to reinvent the wheel, think that something must exist (solution, open source project, code samples).

+4
source share
1 answer

Here is an example of what you need.

  • I assume ChangeItem is a class for storing a single row. So in xaml, you bind the ChangeItem[] property to the ItemsSource your datagrid.

     class ChangeItem { public string Previous { get; set; } public string Current { get; set; } public bool HasDiff { return this.Previous != this.Current; } } 
  • In Xaml, add custom style to your resources

     <Style TargetType="{x:Type DataGridCell}"> <Style.Triggers> <DataTrigger Binding="{Binding HasDiff}" Value="true"> <Setter Property="Background" Value="Red"/> </DataTrigger> </Style.Triggers> </Style> 
  • If you need to support changes in the background and in real time, depending on the changes made. Then execute INotifyPropertyChanged correctly in the ChangeItem class.

  • If you need to have more than two states (HasError / NoErrors), then create a new enumeration representing the states. For instance:

     public enum LineState { Normal, SmallError, MegaError, } 

    And replace the public bool HasDiff { ... } property with something like public LineState State { ... } .

Hope this helps.

+4
source

Source: https://habr.com/ru/post/1332629/


All Articles