2013-05-30 49 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