2016-04-01 10 views
1

Açılan düğmeyle bir ağaç gösterim var. Açılan düğme, ağaca düğümler eklemek için seçenekleri görüntüler. Kullanıcı bir seçenek seçtiğinde, üst kullanıcı denetiminde bir komutla kapatılıp bir komutun işlenmesini istiyorum. FYI catel MVVM framework'ü kullanarak.WPF Ciltleme yuvalanmış liste görünümünde çalışmıyor

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

Ben bağlayıcı bir hata alıyorum bu çalıştırdığınızda:

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

Atasözü türü ve öğe adı için göreli kaynak arama kullanmayı denedim, ancak bu seçeneklerin her ikisi de daha da karışık hata iletileri döndürdü. Açıkçası burada bir şey eksik.

Teşekkürler.

cevap

2

Sorunuzdaki hata iletisine göre, Buttons'ın DataContext öğesi MainWindowViewModel türünde, ancak AddNode komutuna sahip olan sınıf ManageSyncedAccountsViewModel olarak adlandırılır. düğme görebileceği bir yere

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

MainWindowViewModel üzerinde AddNode koyarak deneyin.

+1

Ve "problemi çok uzun sürdüğü ve dikkatlice okumadığı" bir kez daha grevler çıkardı. Çok teşekkürler Ed, beynin cesur bölümü göz ardı ederek sıkışmıştı! – Bitfiddler

+0

@BitFiddler Şerefe! –