简体   繁体   English

选中复选框时更改WPF数据网格行背景色

[英]Change wpf data grid row background color when checkbox is checked

I am trying to change the color of my data grid row when the checkbox of the given row is checked, and when unchecked it should reset the value to the previous one. 当给定行的复选框被选中时,我试图更改数据网格行的颜色,而当未选中该复选框时,应将值重置为前一个值。

I am using MVVM to achieve the above mentioned functionality. 我正在使用MVVM来实现上述功能。

My XAML CODE :- 我的XAML代码:-

 <Window.Resources>
        <Style x:Key="RowStyle"  TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding DataContext.IsChecked, UpdateSourceTrigger=PropertyChanged}" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

 </Window.Resources>

<Grid>
      <DataGrid Name="lbUsers" ItemsSource="{Binding  Data}" CanUserAddRows="False" Grid.Column="1" Grid.Row="1"  SelectedIndex="{Binding SelectionIndexChange, Mode=TwoWay}" AutoGenerateColumns="False">

            <DataGrid.Columns>

                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Width="45" Height="20" Command="{Binding ElementName=lbUsers,Path=DataContext.IsChecked}" ></CheckBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>

        </DataGrid>
</Grid>

Given below is view model code : 下面给出的是视图模型代码:

        public ViewModel ()
        {
            Data = new ObservableCollection<CommonData>
            {

            };

        }



    private ObservableCollection<CommonData> _data;
        public ObservableCollection<CommonData> Data
        {
            get
            {
                if (_data == null)
                {
                    _data = new ObservableCollection<CommonData>()
                    {

                    };

                }

                return _data;
            }
            set
            {
                if (value != this._data)
                {
                    this._data = value;

                    NotifyPropertyChanged("Data");
                }
            }
        }



        private bool _isChecked;
        public bool IsChecked
        {
            get { return _isChecked; }
            set { this._isChecked = value; NotifyPropertyChanged("IsChecked"); }
        } 

Please let me know what wrong am i doing to get the given functionality working. 请让我知道我在做错什么才能使给定的功能正常工作。

Thanks in advance, in case of missing information please let me know. 在此先感谢您,如果缺少信息,请告诉我。

A couple things: 几件事:

You've assigned an x:Key to the style, but are not using it on the DataGrid . 您已经为样式分配了x:Key ,但是没有在DataGrid上使用它。 Remove the key to make it the default style for all DataGridRow , or add this to the grid: 删除键以使其成为所有DataGridRow的默认样式,或将其添加到网格中:

RowStyle="{StaticResource RowStyle}"

Within the DataTrigger binding, you will also need add DataTrigger绑定中,您还需要添加

ElementName=lbUsers

Additionally, your Checkbox is not bound properly -- this is not done through a Command . 此外,您的Checkbox未正确绑定-不能通过Command完成。 You will need to change 您将需要改变

Command={Binding...

To

IsChecked={Binding...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM