无法访问datatemplate中的复选框

时间:2014-05-07 11:17:28

标签: c# wpf

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

我正在尝试访问IsChecked标题模板中定义的CheckBox的{​​{1}}属性。但我发现我无法访问该GroupBox州。我也尝试过使用后面的代码而且它也不可用。有人能在这里给我一个暗示吗?

1 个答案:

答案 0 :(得分:0)

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