有没有办法在Custom Datagrid中显示项目?

时间:2019-06-05 20:47:25

标签: c# wpf xaml inotifypropertychanged

我有一个填充有项目列表的自定义DataGrid,我想在双击它们时更改项目的行颜色。

我正在尝试使用绑定到每个项目的bool属性的DataTriggers来执行此操作,但是它不起作用。 我可以看到bool属性实际上已更改为“ false”,但该更改未反映在UI中。

XAML代码:

<controlGrid:CustomDataGrid 
              Grid.Row="2"              
              Grid.Column="1" 
              MinHeight="100" 
              MaxHeight="650"
              AutoGenerateColumns="False" 
              SelectionUnit="FullRow"
              AlternationCount="2"
              CanUserAddRows ="False"
              HorizontalAlignment="Stretch"
              VerticalScrollBarVisibility="Auto"
              ItemsSource="{Binding Path=Items, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"                  
              SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}">

    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Selected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="False">
                    <Setter Property="Background" Value="Red"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDoubleClick">
            <i:InvokeCommandAction Command="{Binding DoubleClickCommand}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>

C#ViewModel

    private ICollectionView _items;
    public ICollectionView Items
    {
        get { return _items; }
        set
        {
            _cuentas = value;
            NotifyPropertyChanged(nameof(Items));
        }
    }

    private Item selectedItem;
    public Item SelectedItem
    {
        get => selectedItem;
        set
        {
            selectedItem = value;
            SelectedItem.Selected = false;
            NotifyPropertyChanged(nameof(SelectedItem));
        }
     {

  private void DoubleClickInteraction()
  {
      foreach (var item in Items.Cast<Item>().Where(c => c.Id ==    SelectedItem.Id)
      {
          item.SeleccionadaPreviamente = false;
      }
  }

自定义数据网格

public class CustomDataGrid : DataGrid
{
    public static readonly DependencyProperty SelectedItemsListProperty = DependencyProperty.Register("SelectedItemsList", typeof(IList), typeof(CustomDataGrid), new PropertyMetadata(null));
    public static readonly DependencyProperty PreSelectedItemsProperty = DependencyProperty.Register("PreSelectedItems", typeof(IList), typeof(CustomDataGrid), new PropertyMetadata(null));
    public CustomDataGrid()
    {
        this.SelectionChanged += CustomDataGrid_SelectionChanged;
    }

    public void CustomDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        this.SelectedItemsList = this.SelectedItems;
    }

尝试直接在所选项目属性上更改属性,并遍历所有列表,并在项目ID与所选项目匹配时进行更改,但仍然没有任何效果:( 有什么建议吗?

0 个答案:

没有答案