2009-06-30 42 views
1

私はツリービューを作成し、ツリー内の各ノードのチェックボックス、アイコンイメージ、テキストを含むスタックパネルを使用しました。 これらのノードは実行時に作成されます。 私はボタンオブジェクトも持っています。 xamlは以下のとおりです。WPF Treeview - チェックボックスの状態を取得

私が持っている問題は、クリックしてボタンをクリックすると、ツリービューを横切る必要があり、チェックボックスにチェックが入っていれば、いくつかの機能を実行する必要があります。

ツリー内のノードのチェックボックスがチェックされているかどうかを知っている人はいますか?

<Window x:Class="WPF_Explorer_Tree.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:WPF_Explorer_Tree" 
Title="KryptoG" Height="424" Width="815" Loaded="Window_Loaded"> 
<Window.Resources> 
    <local:HeaderConverter x:Key="formatter" /> 
</Window.Resources> 
<Grid> 
    <TreeView x:Name="foldersItem" SelectedItemChanged="foldersItem_SelectedItemChanged" Background="#FFFFFFFF" BorderBrush="#FFFFFFFF" Foreground="#FFFFFFFF" Margin="0,0,236,112" AllowDrop="True" Visibility="Visible"> 
     <TreeView.Resources> 
      <Style TargetType="{x:Type TreeViewItem}"> 
       <Setter Property="HeaderTemplate"> 
        <Setter.Value> 
         <DataTemplate> 
          <StackPanel Name="ST" Orientation="Horizontal"> 
           <CheckBox VerticalAlignment="Center" Name="SelectedCheckBox" IsChecked="False" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" /> 
          <Image Name="img" Width="20" Stretch="Fill" 
            Source="{Binding 
            RelativeSource={RelativeSource 
            Mode=FindAncestor, 
            AncestorType={x:Type TreeViewItem}}, 
            Path=Header, 
            Converter={x:Static local:HeaderToImageConverter.InstanceIcon}}"  
            /> 
           <TextBlock VerticalAlignment="Center" Text="{Binding 
            RelativeSource={RelativeSource 
            Mode=FindAncestor, 
            AncestorType={x:Type TreeViewItem}}, 
            Path=Header, 
            Converter={StaticResource formatter}}" 
            /> 
          </StackPanel> 
         </DataTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </TreeView.Resources> 
    </TreeView> 
    <TreeView HorizontalAlignment="Right" Margin="0,0,12,12" Name="treeView1" Width="204" AllowDrop="True" BorderBrush="White" Foreground="White" /> 
    <Button Height="23" HorizontalAlignment="Left" Margin="12,0,0,70" Name="button1" VerticalAlignment="Bottom" Width="75" Click="button1_Click">Click Me</Button> 
    <Button Height="23" HorizontalAlignment="Left" Margin="267,0,0,69" Name="button2" VerticalAlignment="Bottom" Width="75" Click="button2_Click">Click Me too</Button> 
</Grid> 

+0

どのようなアイテムをあなたのTreeViewに入れていますか? –

+0

TreeViewItems。 それは答えですか?そうでない場合は、他にどのような情報が必要かを教えてください。 – trainer

答えて

7

私はその代わりのViewModelオブジェクトにボックスのにisCheckedプロパティをチェックして結合双方向のデータを作成します。ツリーをナビゲートするよりもずっと簡単です。


編集(尋ねる人の要求あたり):今では、あなた

public class ViewModel : System.ComponentModel.INotifyPropertyChanged 
{ 
    private bool? _isChecekd; 
    public bool? IsChecked 
    { 
     get { return _isChecekd; } 
     set 
     { 
      if (_isChecekd != value) 
      { 
       _isChecekd = value; 
       if (PropertyChanged != null) 
       { 
        PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("IsChecked")); 
       } 
      } 
     } 
    } 
    #region INotifyPropertyChanged Members 
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; 
    #endregion 
} 

ここで(のみにisCheckedプロパティを占めている非常に単純な)例のビューモデルですINotifyPropertyChangedを実装するオブジェクトがある場合は、UI要素のプロパティをバインドできます。したがって、CheckBoxのIsCheckedプロパティをこのプロパティに更新します。これを行うには、最初に何らかの方法でWindow1のDataContextを設定する必要があります(または、TreeView自体でも同様に行うことができます)。

public Window1() 
     { 
      InitializeComponent(); 
      this.DataContext = new ViewModel(); 
     } 

次に、あなたのWindow1.xamlファイルでは、チェックボックスにisCheckedプロパティを更新:あなたのWindow1.xaml.csで、その後

<CheckBox VerticalAlignment="Center" Name="SelectedCheckBox" IsChecked="{Binding Path=IsChecked, Mode=TwoWay}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" /> 

そして、どんなコードであなたが問い合わせできるようにする必要がありますisCheckedの現在価値、あなたは(このがウィンドウ1であるを想定)、それにこの方法を取得することができます:

((ViewModel)this.DataContext).IsChecked 

役に立てば幸い!

1

私はTony Heupelの答えが最良のアプローチだと思いますが、MVVM(Model-View-ViewModel)の設計パターンについて理解する必要があります。私はあなたがこれを読むことをお勧めしますexcellent article by Josh Smith

関連する問題