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

Wpf 将DataTrigger绑定到复选框的IsChecked属性,wpf,binding,datagrid,checkbox,datatrigger,Wpf,Binding,Datagrid,Checkbox,Datatrigger,我相信我想做的很简单,所以我可能只是错过了一些明显的东西 在DataGrid中,我试图绑定一个复选框,以便在选中该复选框时,其行的背景颜色将发生变化。每行都有一个复选框。我基本上实现了我自己的“选择多行”功能。这是一个产品要求,不用问,除此之外,我的其他一切都正常工作,只是选择了一行的视觉指示 我已经读过了,但我缺少答案的地方是,究竟应该把什么作为BooleanPropertyOnObjectBoundToRow。我也看过并尝试过搞乱一个相对的资源,但没有运气 我在代码隐藏中创建了我的网格,但以

我相信我想做的很简单,所以我可能只是错过了一些明显的东西

在DataGrid中,我试图绑定一个复选框,以便在选中该复选框时,其行的背景颜色将发生变化。每行都有一个复选框。我基本上实现了我自己的“选择多行”功能。这是一个产品要求,不用问,除此之外,我的其他一切都正常工作,只是选择了一行的视觉指示

我已经读过了,但我缺少答案的地方是,究竟应该把什么作为BooleanPropertyOnObjectBoundToRow。我也看过并尝试过搞乱一个相对的资源,但没有运气

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

<Style x:Key="MyRowStyle" TargetType="DataGridRow">
      <Style.Triggers>
           <DataTrigger Binding="{Binding IsChecked}" Value="True">
               <Setter Property="Background" Value="Blue"/>
           </DataTrigger>
      </Style.Triggers>
</Style>
有趣的是,我将DataGrid的ItemsSource设置为DataTable,但我的CheckBox列在DataTable中没有相应的列。我只是单独添加模板列,可能是底层存储的缺乏影响了这一点


在任何情况下,如果你需要任何更多的信息,请让我知道。谢谢

下面是一个使用C类而不是数据集的示例

Xaml

C


谢谢你的回答,虽然我在使用数据集方面没有选择,但你的帖子告诉我,我确实需要某种形式的底层存储来保存这个布尔值,所以我所做的就是在我的DataTable中动态添加一个布尔列,并将我的复选框和DataTrigger绑定到它,等等!
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);
<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>
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); }
    }
}