2014-11-22 2 views
0

이 xaml 있습니다. 그리고 내가 체크 된 다른 모든 체크 박스를 선택 해제해야합니다. 나는 단 하나만 검사하도록 허용하는 다른 말. 런타임에 TreeViewItems를 추가합니다. 런타임에xaml C# treeview checkboxex 다른 모든 확인란을 선택 취소하십시오.

<TreeView Name="treeView_max" > 
    <TreeView.Resources> 
     <Style TargetType="{x:Type TreeViewItem}"> 
      <Setter Property="HeaderTemplate"> 
       <Setter.Value> 
        <DataTemplate> 
         <StackPanel Orientation="Horizontal" > 
          <CheckBox Name="chk" Margin="2" Tag="{Binding}" Checked="checkBox_Checked"> 
          </CheckBox> 
         </StackPanel> 
        </DataTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </TreeView.Resources> 
</TreeView> 

추가 TreeViewItems : 당신에게 하나 개의 옵션 만의 동작을 얻을 것이다

foreach (Genesyslab.Desktop.Modules.Core.Model.BusinessAttributes.IDispositionCodeValue item in listOfDispositionCodeValueItemsControl.Items) 
{ 
    TreeViewItem newChild2 = new TreeViewItem(); 
    newChild2.Header = item.DisplayName.Remove(0,item.DisplayName.IndexOf("-") + 1); 
    treeView_max.Items.Add(newChild2);..........` 

private void checkBox_Checked(object sender, RoutedEventArgs e) 
{ 
    try 
    { 
     //uncheck all checkboxes except selected one   
    } 
    catch (Exception es) 
    { 
     MessageBox.Show(es.ToString()); 
    } 
} 

답변

1

대신 같은 그룹에 속해 RadioButton 컨트롤을 사용할 수 있습니다, 한 번에 선택할 수 있습니다.

그런 다음 그 RadioButton 년대 대신에 CheckBox 컨트롤을 표시 할 수있는 컨트롤 템플릿을 편집하고, 부모 RadioButtonCheckBoxIsChecked 속성을 바인딩합니다. 이제 CheckBox을 "확인"하면 다른 모든 CheckBox 컨트롤이 선택 취소됩니다.

<TreeView Name="treeView_max" > 
    <TreeView.Resources> 
     <Style TargetType="{x:Type TreeViewItem}"> 
      <Setter Property="HeaderTemplate"> 
       <Setter.Value> 
        <DataTemplate> 
         <StackPanel Orientation="Horizontal" > 
          <RadioButton Name="chk" Margin="2" Tag="{Binding}" GroupName="SomeGroup"> 
           <RadioButton.Template> 
            <ControlTemplate> 
             <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=RadioButton}}" /> 
            </ControlTemplate> 
           </RadioButton.Template> 
          </RadioButton> 
         </StackPanel> 
        </DataTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </TreeView.Resources> 
</TreeView> 

사용 장소에주의하십시오. 사용자는 하나의 옵션 만 선택할 수있는 경우 RadioButton을보고, 여러 옵션을 선택할 수있는 CheckBox을 보는 데 익숙합니다.

관련 문제