2016-05-24 28 views
0

嗨,我是WPF開發的新手,並遇到問題關於綁定到公共變量TextBlock元素。WPF ContextMenu - 沒有與ItemTemplate字符串

<ListBox.ContextMenu> 
    <ContextMenu ItemsSource="{Binding ActionsView}"> 
     <ContextMenu.ItemTemplate> 
      <DataTemplate>         
      <TextBlock Text="{Binding Name}" /> 
      </DataTemplate> 
     </ContextMenu.ItemTemplate>  
    </ContextMenu> 

動作視圖是行動項目的公開觀察集合各持一個名字是因爲名稱公衆開放。所以通常情況下應該沒有問題。如果我正確點擊我的項目,我會得到一個空的ContextMenu,其中包含正確的條目數,但沒有任何文本。

picture of the empty ContextMenu

public class Action : INotifyPropertyChanged 
{ 
    public string Name;   

    public ContextAction(string name) 
    { 
     Name = name;    
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
} 

這將是非常好的,如果有人可以幫助我解決這個問題。

+0

據我所知,在公共領域的結合發動機無法正常工作。嘗試添加getters和setter。 –

+0

是的,這是整個問題與getters和setter everthing是好的;) – Tooly

回答

0

您需要實現屬性,而不是綁定工作的字段。就像這樣:

public string Name { get; set }; 
1

溶液設置的getter的setter方法就是這樣:)

public class ContextAction : INotifyPropertyChanged 
{ 
    public string _name; 

    public ContextAction(string name) 
    { 
     _name = name;    
    } 


    public string Name 
    { 
     get { return _name; } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

}