Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法访问数据模板中的复选框_C#_Wpf - Fatal编程技术网

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

C# 无法访问数据模板中的复选框,c#,wpf,C#,Wpf,我正在尝试访问GroupBox标题模板中定义的复选框的IsChecked属性。但我发现我无法访问该复选框状态。我也尝试在代码隐藏中使用,但它也不可用。有人能给我一个提示吗?您的XAML将如下所示 <GroupBox x:Name="CrashGenerationGroupBox" Header="Crash Generation" Margin="5" FontSize="18" FontWeight="SemiBold"> <GroupBox.HeaderTempla

我正在尝试访问GroupBox标题模板中定义的复选框的IsChecked属性。但我发现我无法访问该复选框状态。我也尝试在代码隐藏中使用,但它也不可用。有人能给我一个提示吗?

您的XAML将如下所示

<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>
后面的代码是这样的:

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