2016-04-01 36 views
1

私はドロップダウンボタン付きのツリービューを持っています。ドロップダウンボタンは、ノードをツリーに追加するためのオプションを表示します。ユーザーがオプションを選択すると、コマンドを起動して親のユーザーコントロール内のコマンドで処理する必要があります。 catel MVVMフレームワークを使用してFYIWPFネストされたリストビューでバインディングが機能しない

XAML

<Grid Margin="10"> 
    <TreeView x:Name="CriteriaTreeView" ItemsSource="{Binding Criteria}"> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="SelectedItemChanged"> 
     <catel:EventToCommand Command="{Binding NodeSelectionChanged}" CommandParameter="{Binding ElementName=CriteriaTreeView, Path=SelectedItem}" DisableAssociatedObjectOnCannotExecute="False" /> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
    <TreeView.Resources> 
     <HierarchicalDataTemplate DataType="{x:Type self:Group}" ItemsSource="{Binding Items}"> 
     <StackPanel Orientation="Horizontal"> 
      <ComboBox ItemsSource="{Binding OperatorOptions}" SelectedValue="{Binding SelectedOperator}" DisplayMemberPath="DisplayText" SelectedValuePath="Value" /> 
     </StackPanel> 
     </HierarchicalDataTemplate> 
     <DataTemplate DataType="{x:Type self:Leaf}"> 
     <StackPanel Orientation="Horizontal"> 
      <TextBlock Text="Where Account Number " /> 
      <ComboBox ItemsSource="{Binding OperatorOptions}" SelectedValue="{Binding SelectedOperator}" DisplayMemberPath="DisplayText" SelectedValuePath="Value" /> 
      <TextBox Text="{Binding Value}" Width="50" /> 
     </StackPanel> 
     </DataTemplate> 
     <DataTemplate DataType="{x:Type self:NodeFactory}"> 
     <xctk:DropDownButton Content="Add Condition" IsOpen="{Binding IsOpen}"> 
      <xctk:DropDownButton.DropDownContent> 
      <ListView ItemsSource="{Binding AddOptions}" SelectedValue="{Binding SelectedOption}"> 
       <i:Interaction.Triggers> 
       <i:EventTrigger EventName="PreviewMouseLeftButtonDown"> 
        <catel:EventToCommand Command="{Binding Source={x:Reference ManageSyncControl}, Path=DataContext.AddNode}" DisableAssociatedObjectOnCannotExecute="False" /> 
       </i:EventTrigger> 
       </i:Interaction.Triggers> 
       <ListView.ItemTemplate> 
       <DataTemplate> 
        <WrapPanel> 
        <TextBlock Text="{Binding DisplayText}" /> 
        </WrapPanel> 
       </DataTemplate> 
       </ListView.ItemTemplate> 
      </ListView> 
      </xctk:DropDownButton.DropDownContent> 
     </xctk:DropDownButton> 
     </DataTemplate> 
    </TreeView.Resources> 
    </TreeView> 
</Grid> 

のViewModel

public class ManageSyncedAccountsViewModel: ViewModelEventBase { 
 
     public ManageSyncedAccountsViewModel(IEventAggregator eventAggregator): base(eventAggregator) { 
 
     AddNode = new Command(OnAddNode); 
 
     NodeSelectionChanged = new Command <Node> (OnNodeSelectionChanged); 
 

 
     var root = new Group("Root"); 
 
     var g1 = new Group("Group 1"); 
 
     g1.AddNode(new Leaf("Leaf 1")); 
 
     g1.AddNode(new Leaf("Leaf 2")); 
 
     var g2 = new Group("Group2"); 
 
     g2.AddNode(new Leaf("Leaf 3")); 
 
     g2.AddNode(new Leaf("Leaf 4")); 
 
     root.AddNode(g1); 
 
     root.AddNode(g2); 
 
     root.AddNode(new Leaf("Leaf 5")); 
 

 
     Criteria = new List <Group> { 
 
      root 
 
     }; 
 
     } 
 

 
     private void OnNodeSelectionChanged(Node target) { 
 
     SelectedNode = target; 
 
     } 
 

 
     private void OnAddNode() { 
 
     Console.Out.WriteLine("WOOHOO"); 
 
     } 
 

 
     public List <Group> Criteria { 
 
     get { 
 
      return GetValue < List <Group>> (CriteriaProperty); 
 
     } 
 
     set { 
 
      SetValue(CriteriaProperty, value); 
 
     } 
 
     } 
 

 
     public static readonly PropertyData CriteriaProperty = RegisterProperty(nameof(Criteria), typeof(List <Group>)); 
 

 

 
     public Node SelectedNode { 
 
     get { 
 
      return GetValue <Node> (SelectedNodeProperty); 
 
     } 
 
     set { 
 
      SetValue(SelectedNodeProperty, value); 
 
     } 
 
     } 
 

 
     public static readonly PropertyData SelectedNodeProperty = RegisterProperty(nameof(SelectedNode), typeof(Node)); 
 

 
     public Command AddNode { 
 
     get; 
 
     private set; 
 
     } 
 
     public Command <Node> NodeSelectionChanged { 
 
     get; 
 
     private set; 
 
     } 
 
    }

私はこれを実行すると、私はバインディングのエラーを取得:

System.Windows.Data Error: 40 : BindingExpression path error: 'AddNode' property not found on 'object' ''MainWindowViewModel' (HashCode=-1718218621)'. BindingExpression:Path=DataContext.AddNode; DataItem='ManageSyncedAccountsView' (Name='ManageSyncControl'); target element is 'EventToCommand' (HashCode=6678752); target property is 'Command' (type 'ICommand')

祖先型と要素名を検索する相対的なソースを使用しようとしましたが、これらのオプションの両方がさらに混乱したエラーメッセージを返しました。明らかに私はここで何かを逃している。

ありがとうございました。

答えて

2

あなたの質問のエラーメッセージによると、ButtonのDataContextはMainWindowViewModelタイプですが、AddNodeコマンドを所有するクラスはManageSyncedAccountsViewModelと呼ばれています。

System.Windows.Data Error: 40 : BindingExpression path error: 'AddNode' property not found on 'object' ''MainWindowViewModel' (HashCode=-1718218621)'. BindingExpression:Path=DataContext.AddNode; DataItem='ManageSyncedAccountsView' (Name='ManageSyncControl'); target element is 'EventToCommand' (HashCode=6678752); target property is 'Command' (type 'ICommand')

ボタンがそれを見ることができるMainWindowViewModelAddNodeを入れてみてください。

+1

「問題が長すぎて慎重に読んでいない」と表示された場合は、もう一度ストライキを実行します。多くの感謝のエド、大胆な部分を無視して脳がついてしまった! – Bitfiddler

+0

@BitFiddler乾杯! –

関連する問題