2013-05-30 48 views
0

我试图做一个使用Fluent功能区的下拉按钮来显示我的数据库中可用的所有表的菜单。该菜单是通过使用从db中检索到的列表生成的,然后绑定到下拉按钮的itemssource。菜单中的命令没有执行,没有错误

到目前为止,菜单已生成,表名正确显示。

现在我想每一个菜单项绑定到一个命令,将使用命令LoadTableCommand将于当前菜单项的标题,并用它来加载正确的表加载它们在CurrentTable。

这里是问题出现的地方,命令没有执行,我似乎没有得到任何错误或线索为什么这是(或不)发生。

我现在唯一能想到的就是可以在viewmodel本身中绑定命令,然后将其分配给ItemsSource,但我还没有找到如何做到这一点,因为我最喜欢的搜索引擎没有找到了我的解决方案..

PS:DataManager类可以正常工作,如果这可能会引起你的关注。

有没有人有一个想法,我要去哪里错了?

任何帮助,将不胜感激,一如既往;)

MainWindow.xaml

... 
       <Fluent:DropDownButton Header="Table" 
             ItemsSource="{Binding TablesList}"> 
        <Fluent:DropDownButton.ItemContainerStyle> 
         <Style TargetType="MenuItem"> 
          <Setter Property="Command" Value="{Binding LoadTableCommand}" /> 
          <Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource Self}, Path=Header}" /> 
         </Style> 
        </Fluent:DropDownButton.ItemContainerStyle> 
       </Fluent:DropDownButton> 
... 

MainViewModel.cs

... 
    private DataManager dataManager = new DataManager("Data Source=db.sqlite"); 


    /// <summary> 
    /// Initializes a new instance of the MainViewModel class. 
    /// </summary> 
    public MainViewModel() 
    { 
     TablesList = dataManager.GetTables(); 
     CurrentTable = dataManager.GetTable("PriceList"); 
    } 

    /// <summary> 
    /// The <see cref="TablesList" /> property's name. 
    /// </summary> 
    public const string TablesListPropertyName = "TablesList"; 

    private List<string> _tablesList = new List<string>(); 

    /// <summary> 
    /// Sets and gets the TablesList property. 
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary> 
    public List<string> TablesList 
    { 
     get 
     { 
      return _tablesList; 
     } 

     set 
     { 
      if (_tablesList == value) 
      { 
       return; 
      } 

      RaisePropertyChanging(TablesListPropertyName); 
      _tablesList = value; 
      RaisePropertyChanged(TablesListPropertyName); 
     } 
    } 

    /// <summary> 
    /// The <see cref="CurrentTable" /> property's name. 
    /// </summary> 
    public const string CurrentTablePropertyName = "CurrentTable"; 

    private DataTable _currentTable = new DataTable(); 

    /// <summary> 
    /// Sets and gets the CurrentTable property. 
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary> 
    public DataTable CurrentTable 
    { 
     get 
     { 
      return _currentTable; 
     } 

     set 
     { 
      if (_currentTable == value) 
      { 
       return; 
      } 

      RaisePropertyChanging(CurrentTablePropertyName); 
      _currentTable = value; 
      RaisePropertyChanged(CurrentTablePropertyName); 
     } 
    } 

    private RelayCommand<string> _loadTable; 

    /// <summary> 
    /// Gets the LoadTableCommand. 
    /// </summary> 
    public RelayCommand<string> LoadTableCommand 
    { 
     get 
     { 
      return _loadTable 
       ?? (_loadTable = new RelayCommand<string>(
             table => LoadTable(table))); 
     } 
    } 

    private void LoadTable(string name) 
    { 
     Console.WriteLine(name); 
     // CurrentTable = dataManager.GetTable(name); 
    } 
... 
+1

使用Snoop在运行时检查您的DataContext和绑定。 http://snoopwpf.codeplex.com/ < - 它是一个非常好的工具,易于处理:) – blindmeis

+0

我从来没有这样做过,你可能会指出我在正确的方向上,我可以找到某种教程或解释这个?感谢:) – Kryptoxx

+0

感谢您的工具,我不知道它的存在,但这会真的为我节省大量的时间,而你们,因为我会有更少的问题:P – Kryptoxx

回答

1

您TablesList是字符串列表 - 一个字符串有没有属性LoadTableCommand。所以你应该纠正你的命令绑定。使用Element绑定或RelativeSource绑定将树遍历到正确的datacontext。

+0

谢谢你,我以为我是自动的绑定到MainViewModel,但我实际上绑定到TablesList。我将其替换为: 虽然我想知道这是否是什么你是指或正确的用法? 感谢您的反馈,非常感谢! – Kryptoxx

+0

是的,这就是我的意思,我总是忘记第三个选项 - 你使用的一个 - StaticResource :),你总是必须检查2件事情,当绑定不工作 - > 1. DataContext 2.绑定路径:) – blindmeis