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

<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; }
   }

 

 

posted @ 2022-12-28 17:04  simadi  阅读(38)  评论(0编辑  收藏  举报