[英]WPF ContextMenu - no String with ItemTemplate
Hi I am new to WPF Development and run into a problem Regarding Binding al public variable to a TextBlock element. 嗨,我是WPF开发的新手,并遇到了有关将所有公共变量绑定到TextBlock元素的问题。
<ListBox.ContextMenu>
<ContextMenu ItemsSource="{Binding ActionsView}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
Action View is a public Observable collection of Action Items each holds a name which is public accessible as Name. 动作视图是可观察到的公共“动作项”集合,每个动作项都有一个名称,可以作为“名称”进行公共访问。 So normally there should be no Problem. 因此,通常应该没有问题。 If I am right clicking on my Item, I get an empty ContextMenu with the correct number of entry's but without any text. 如果我在我的项目上单击鼠标右键,则会得到一个空的ContextMenu,其中的条目数正确,但没有任何文本。
picture of the empty ContextMenu 空的ContextMenu的图片
public class Action : INotifyPropertyChanged
{
public string Name;
public ContextAction(string name)
{
Name = name;
}
public event PropertyChangedEventHandler PropertyChanged;
}
It would be really nice if somebody could help me with this problem. 如果有人可以帮助我解决这个问题,那就太好了。
The solution was setting the getters an setters that's it :) 解决方案是将吸气剂设置为吸气剂:)
public class ContextAction : INotifyPropertyChanged
{
public string _name;
public ContextAction(string name)
{
_name = name;
}
public string Name
{
get { return _name; }
}
public event PropertyChangedEventHandler PropertyChanged;
} }
You need to implement property, not a field for bindings to work. 您需要实现属性,而不是要进行绑定的字段。 Like this: 像这样:
public string Name { get; set };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.