无法访问数据模板中的复选框

本文关键字:复选框 访问 数据 | 更新日期: 2023-09-27 18:02:03

<GroupBox x:Name="CrashGenerationGroupBox" Header="Crash Generation"  Margin="5" FontSize="18" FontWeight="SemiBold">
   <GroupBox.HeaderTemplate>
      <DataTemplate>
         <StackPanel Orientation="Horizontal">
            <CheckBox x:Name="cbHeaderCrashGeneration"/>
            <TextBlock Text="{Binding}"/>
         </StackPanel>
      </DataTemplate>
   </GroupBox.HeaderTemplate>
   <StackPanel Orientation="Horizontal">
      <RadioButton GroupName="CrashGeneration" Content="Oscar" IsEnabled="{Binding ElementName=cbHeaderCrashGeneration, Path=IsChecked}"/>
      <RadioButton GroupName="CrashGeneration" Content="CrashSimulator" IsEnabled="{Binding ElementName=cbHeaderCrashGeneration, Path=IsChecked}"/>
   </StackPanel>
</GroupBox>

我试图访问GroupBox的头模板中定义的CheckBoxIsChecked属性。但是我发现我不能进入CheckBox状态。我也试过在后面的代码中使用,它也不可用。有人能给我点提示吗?

无法访问数据模板中的复选框

您的XAML将是这样的…

<Grid>
    <DataGrid x:Name="datagrid1" AutoGenerateColumns="True">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Select Value">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox Name="Chk" Tag="{Binding}" Checked="Chk_Checked"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.HeaderTemplate>
                    <DataTemplate>
                        <CheckBox Name="ChkAll" Checked="ChkAll_Checked"  Unchecked="ChkAll_Unchecked"  IsThreeState="False" Padding="4,3,4,3" HorizontalContentAlignment="Center" HorizontalAlignment="Center"/>
                    </DataTemplate>
                </DataGridTemplateColumn.HeaderTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

后面的代码是这样的:

public partial class MainWindow : Window
{
    private ObservableCollection<customer> custcol;
    public ObservableCollection<customer> custCol
    {
        get { return custcol; }
        set
        {
            custcol = value;
        }
    }
    public MainWindow()
    {
        InitializeComponent();
        custcol = new ObservableCollection<customer>();
        custCol.Add(new customer { custID = 1, custName = "1", Status = "InActive", Flag = true });
        custCol.Add(new customer { custID = 2, custName = "2", Status = "InActive", Flag = false });
        custCol.Add(new customer { custID = 3, custName = "3", Status = "InActive", Flag = false });
        datagrid1.ItemsSource = this.custCol;
    }
    private void ChkAll_Checked(object sender, RoutedEventArgs e)
    {
    }
    private void ChkAll_Unchecked(object sender, RoutedEventArgs e)
    {
    }
    private void Chk_Checked(object sender, RoutedEventArgs e)
    {
        switch (((sender as CheckBox).Tag as customer).custID)
        {
            case 1: break;
            case 2: break;
            case 3: break;
        }
    }
}
public class customer : INotifyPropertyChanged
{
    public object obj { get; set; }
    public int custID { get; set; }
    private string custname;
    public string custName
    {
        get { return custname; }
        set
        {
            custname = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("custName"));
            }
        }
    }
    public DateTime startTime { get; set; }
    public DateTime endTime { get; set; }
    private string status;
    public string Status
    {
        get { return status; }
        set
        {
            status = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Status"));
            }
        }
    }
    private string duration;
    public string Duration
    {
        get { return duration; }
        set
        {
            duration = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Duration"));
            }
        }
    }
    public bool Flag { get; set; }
    public event PropertyChangedEventHandler PropertyChanged;
}