首页 文章

将DataTrigger绑定到复选框的IsChecked属性

提问于
浏览
6

我相信我所要做的就是“简单”,所以我可能只是错过了一些明显的东西 .

在DataGrid中,我试图绑定一个CheckBox,以便在选中它时,其行的Background颜色将会改变 . 每行都有一个CheckBox . 我基本上实现了我自己的select-multiple-rows功能(这是产品要求,不要问),而且我还有其他所有工作,但这是所选行的可视化指示 .

我读过this question但我缺少答案的地方是"BooleanPropertyOnObjectBoundToRow" . 我也看了this question并尝试弄乱了一个RelativeSource,但没有运气 .

我在我的代码隐藏中创建了我的网格,但这是我用于行的当前样式(它定义了我的DataTrigger):

<Style x:Key="MyRowStyle" TargetType="DataGridRow">
      <Style.Triggers>
           <DataTrigger Binding="{Binding IsChecked}" Value="True">
               <Setter Property="Background" Value="Blue"/>
           </DataTrigger>
      </Style.Triggers>
</Style>

现在在我的代码隐藏中,我创建了我的DataGridTemplateColumn并使用Factory来创建我的复选框,这是我的Binding相关代码:

Binding checkBinding = new Binding("IsChecked");
checkBinding.Mode = BindingMode.OneWayToSource;
RelativeSource relativeSource = new RelativeSource();
relativeSource.AncestorType = typeof(DataGridRow);
relativeSource.Mode = RelativeSourceMode.FindAncestor;
checkBinding.RelativeSource = relativeSource;
factory.SetBinding(CheckBox.IsCheckedProperty, checkBinding);

可能感兴趣的是我将DataGrid的ItemsSource设置为DataTable,但我的CheckBox列在DataTable中没有相应的列 . 我只是单独添加模板列,也许缺少底层存储会影响这个?

在任何情况下,如果您需要更多信息,请告诉我 . 谢谢!

1 回答

  • 2

    这是一个使用C#类而不是DataSet的示例 .

    XAML

    <Page.Resources>
        <Style x:Key="RowStyle" TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}" Value="True">
                    <Setter Property="Background" Value="Blue"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Page.Resources>
    
    <Page.DataContext>
        <Samples:DataGridRowHighlightViewModels/>
    </Page.DataContext>
    
    <Grid>
        <DataGrid ItemsSource="{Binding Items}" RowStyle="{StaticResource RowStyle}" CanUserAddRows="False" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridCheckBoxColumn Header="Selected" Binding="{Binding IsChecked}"/>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
    

    C#

    public class DataGridRowHighlightViewModels
    {
        public DataGridRowHighlightViewModels()
        {
            Items = new List<DataGridRowHighlightViewModel>
                        {
                            new DataGridRowHighlightViewModel {Name = "one"},
                            new DataGridRowHighlightViewModel {Name = "two"},
                            new DataGridRowHighlightViewModel {Name = "three"},
                            new DataGridRowHighlightViewModel {Name = "four"},
                        };
        }
        public IEnumerable<DataGridRowHighlightViewModel> Items { get; set; } 
    }
    
    // ViewModelBase and Set() give INotifyPropertyChanged support (from MVVM Light)
    public class DataGridRowHighlightViewModel : ViewModelBase 
    {
        private bool _isChecked;
        public bool IsChecked
        {
            get { return _isChecked; }
            set { Set(()=>IsChecked, ref _isChecked, value); }
        }
    
        private string _name;
        public string Name
        {
            get { return _name; }
            set { Set(()=>Name, ref _name, value); }
        }
    }
    

相关问题