【发布时间】:2020-08-07 14:52:35
【问题描述】:
我有一个带有多个计数器的视图模型,这些计数器用于 serval 方法。 在视图模型中,还有一个类 MenuItem 的集合,其中包含在功能区中创建动态菜单项的信息。在其中一些菜单项上,我想通过徽章显示计数器。
但要做到这一点,我需要将徽章绑定到计数器属性。 在我的 menuitem 类中,我有绑定的路径,但是我怎样才能让我的 menuitem 模板绑定到它自己绑定中的路径。
例子被简化了
public class ViewmodelSample
{
public int counter1 { get; set; }
public ICollection<MenuItem> MenuItems { get; set; } = new ObservableCollection<MenuItem>();
public void Sample()
{
MenuItems.Add(new MenuItem()
{
Name = "Test button",
CounterPath = "counter1"
});
}
public class MenuItem
{
public string Name { get; set; }
public string CounterPath { get; set; }
}
}
<ItemsControl ItemsSource="{Binding Path=MenuItems}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Path={Binding CounterPAth}}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
【问题讨论】:
-
MenuItem类应该实现INotifyPropertyChanged以触发Binding
标签: c# wpf mvvm data-binding