首页 > 其他分享 >WPF - DataGridCheckBoxColumn: How to give ElementName to DataGridCell and CheckBox therein?

WPF - DataGridCheckBoxColumn: How to give ElementName to DataGridCell and CheckBox therein?

时间:2022-12-28 17:14:00浏览次数:43  
标签:DataGridCell therein ElementName Name ID Persons ViewMode new public

<Window.Resources>
         <Style x:Key="cell" TargetType="DataGridCell">
             <Setter Property="Background" Value="LightBlue"/>
         </Style>
     </Window.Resources>
     <StackPanel>
         <TextBlock Name="tb" Text="hello" Width="150" Background="LightSteelBlue"/>
         <DataGrid Name="dg" ItemsSource="{Binding Persons}" AutoGenerateColumns="False"  CellStyle="{StaticResource cell}">
             <DataGrid.Resources>
                 <Style x:Key="BackgroundColourStyle" TargetType="{x:Type CheckBox}">
                     <Style.Triggers>
                         <Trigger Property="IsChecked" Value="True">
                             <Setter Property="Background" Value="LightGreen" />
                         </Trigger>
                     </Style.Triggers>
                 </Style>
             </DataGrid.Resources>
             <DataGrid.Columns>
                 <DataGridTextColumn Header="ID" Binding="{Binding ID}"  />
                 <DataGridTextColumn x:Name="Name1" Header="Name" Binding="{Binding Name}"/>
                 <DataGridCheckBoxColumn x:Name="check" Header="{Binding ElementName=Name1,Path= Header}"  Binding="{Binding Check}"  ElementStyle="{StaticResource BackgroundColourStyle }"/>
                 <DataGridTemplateColumn x:Name="Check" >
                     <DataGridTemplateColumn.Header >
                         <CheckBox Content="SelectAll" x:Name="headerCheckBox"  Background="{Binding ElementName=tb,Path=Background}" Width="{Binding ElementName=tb ,Path=Width}"/>
                     </DataGridTemplateColumn.Header>
                     <DataGridTemplateColumn.CellTemplate >
                         <DataTemplate>
                             <CheckBox x:Name="CheckSelected" Background="Orange" IsChecked="{Binding IsChecked,ElementName=headerCheckBox,Mode=OneWay}"/>
                         </DataTemplate>
                     </DataGridTemplateColumn.CellTemplate>
                 </DataGridTemplateColumn>
             </DataGrid.Columns>
         </DataGrid>
     </StackPanel>
public partial class MainWindow : Window
   {
     public ViewMode viewmodel;
     public MainWindow()
     {
       InitializeComponent();
       viewmodel = new ViewMode();
    
       this.DataContext = viewmodel;
     }
    
   }
   public class ViewMode : INotifyPropertyChanged
   {
     public ObservableCollection<Person> Persons { get; set; }
     public ViewMode()
     {
       Persons = new ObservableCollection<Person>();
       Persons.Add(new Person { ID = 1, Name = "man", Check=true});
       Persons.Add(new Person { ID = 2, Name = "john", Check = true});
       Persons.Add(new Person { ID = 3, Name = "jo" , Check = false });
     }
       
     protected void OnPropertyChanged(string porpName)
     {
       var temp = PropertyChanged;
       if (temp != null)
         temp(this, new PropertyChangedEventArgs(porpName));
     }
     public event PropertyChangedEventHandler PropertyChanged;
   }
   public class Person
   {
     public int ID { get; set; }
     public String Name { get; set; }
     public bool Check { get; set; }
   }

 

 

标签:DataGridCell,therein,ElementName,Name,ID,Persons,ViewMode,new,public
From: https://www.cnblogs.com/simadi/p/17010573.html

相关文章