如何将DataGrid行的IsSelected属性绑定到源属性



我有一个DataGrid,并将其项目源绑定到下面的类列表。如何将数据网格行IsSelected属性绑定到类IsChecked属性?

public class DeckInt : INotifyPropertyChanged
{
public int id { get; set; }
public string name { get; set; }
public bool isChecked { get; set; }

public int Id { get { return id; } set { id = value; OnPropertyChanged(nameof(Id)); } }
/// <summary>
/// Group name of the weight item
/// </summary>
public string Name { get { return name; } set { name = value; OnPropertyChanged(nameof(Name)); } }
public bool IsChecked { get { return isChecked; } set { isChecked = value; OnPropertyChanged(nameof(IsChecked)); } }
public event PropertyChangedEventHandler PropertyChanged;
//When propert changes, notify
protected virtual void OnPropertyChanged(string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

必须通过定义一个针对DataGridRowStyle来配置Binding:

<DataGrid>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="IsSelected"
Value="{Binding IsChecked}" />
</DataGrid.RowStyle>
</DataGrid>

最新更新