2015-05-27 15 views
0

我有這樣的風格:需要協助解決一個問題BindingExpression

   <DataTemplate> 
        <Button 
         Width="44" 
         Height="24" 
         VerticalAlignment="Top" 
         VerticalContentAlignment="Center" 
         HorizontalAlignment="Left" 
         HorizontalContentAlignment="Center" 
         Command="{Binding UninspectedPrintSelectedCommand}" 
         CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Content}"> 

但命令無法正常工作。縱觀輸出窗口產生此問題:

System.Windows.Data Error: 40 : BindingExpression path error: 'UninspectedPrintSelectedCommand' property not found on 'object' ''String' (HashCode=701007577)'. BindingExpression:Path=UninspectedPrintSelectedCommand; DataItem='String' (HashCode=701007577); target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand') 

視圖模型的ICommand屬性:

public ICommand UninspectedPrintSelectedCommand 
    { 
     get 
     { 
      return new DelegateCommand<object>((print) => 
      { 
       string printName = print.ToString(); 
       int indexOfX = printName.IndexOf('x'); 
       Row = DiePrint.GetRow(printName); 
       Col = DiePrint.GetCol(printName); 

       if (diePrint == null) { diePrint = new DiePrint(Row + "x" + Col); } 
       else 
       { 
        diePrint.Row = Convert.ToInt32(row); 
        diePrint.Col = Convert.ToInt32(col); 
       } 
       LoadMap(); 
      }); 
     } 
    } 

我不知道如何解決這個問題。 Button的Command屬性如何被解釋爲一個字符串?

如果這意味着什麼,這是我的App.xaml文件,而不是主窗口。

+0

代碼的財產? –

+0

要綁定到'Button'的'DataContext',這將要被設置爲用於實例化使用'DataTemplate'相關聯的視圖的對象。基於輸出,DataTemplate必須用於字符串。 – Xavier

+0

@MatthewFrontino我還說,我試圖綁定到這個問題 – kformeck

回答

1

我想你想要的是讓你的綁定使用視覺樹中更高的源代碼,其中DataContext被設置爲你想要的視圖模型,而不是設置爲字符串。

例如,假設有更高Grid了正確的數據情況下,你可以使用這樣的結合:

{Binding DataContext.UninspectedPrintSelectedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}} 

更換Grid的東西,會可靠地在視覺樹在正確的水平。

+0

我換我的命令綁定到這一點,並列表框代替電網(因爲這個按鈕是ListBox的的ItemTemplate DataTemplate的財產),並得到如下的輸出信息: – kformeck

+0

BindingExpression路徑錯誤:在「對象」「」列表框中沒有發現「DiePrintNav」屬性'(Name ='lbxUninspectedPrints')''。 BindingExpression:路徑= DiePrintNav.UninspectedPrintSelectedCommand; DataItem ='ListBox'(Name ='lbxUninspectedPrints');目標元素是'Button'(Name ='');目標屬性是「命令」(類型「的ICommand」) – kformeck

+0

對不起,我得到的結合稍有不當的路徑,它需要有'DataContext'在命令名稱的前面。更新我的例子。 – Xavier