【问题标题】:Binding a DataTrigger to the IsChecked property of a checkbox将 DataTrigger 绑定到复选框的 IsChecked 属性
【发布时间】:2012-04-12 11:46:23
【问题描述】:

我相信我正在尝试做的事情已经足够“简单”了,所以我可能只是遗漏了一些明显的东西。

在 DataGrid 中,我试图绑定一个 CheckBox,以便在选中它时,其行的背景颜色会改变。每行都有一个复选框。我基本上是在实现我自己的选择多行功能(这是产品要求,不要问),除了所选行的视觉指示外,我还有其他一切工作。

我已经阅读了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 并使用工厂来创建我的复选框,这是我的绑定相关代码:

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 中没有对应的列。我只是单独添加了模板列,也许缺少底层存储会影响这一点?

无论如何,如果您需要更多信息,请告诉我。谢谢!

【问题讨论】:

    标签: wpf binding datagrid checkbox datatrigger


    【解决方案1】:

    这是一个适用于我使用 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); }
        }
    }
    

    【讨论】:

    • 感谢您的回答,虽然我没有选择使用 DataSet,但您的帖子确实告诉我,我确实需要某种形式的底层存储来保存这个布尔值,所以我所做的是我动态地将一个布尔列附加到我的 DataTable 并将我的复选框和 DataTrigger 绑定到那个,等等!
    猜你喜欢
    • 1970-01-01
    • 2020-11-16
    • 2016-06-22
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    相关资源
    最近更新 更多