簡體   English   中英

如何將 DataGrid 行的 IsSelected 屬性綁定到源屬性

[英]How to bind IsSelected property of DataGrid row to a source property

我有一個 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));
        }
    }

您必須通過定義以DataGridRow為目標的Style來配置Binding

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM