[英]How to bind ComboBoxItem header to the sibling ListView's items count of Combobox control
Why my first item of Combobox display nothing? 为什么我的组合框的第一项什么都不显示? Anything wrong with my binding?
我的装订有问题吗?
xaml: XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<x:Array x:Key="SettingsContextMenuItems" Type="system:Object">
<MenuItem Header="{Binding ElementName=MainDataGrid, Path=Items.Count}"/>
<MenuItem Header="TESTA"/>
<MenuItem Header="TESTB"/>
</x:Array>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<StackPanel>
<ComboBox ItemsSource="{StaticResource SettingsContextMenuItems}" />
</StackPanel>
<StackPanel Grid.Row="1">
<ListView x:Name="MainDataGrid" ItemsSource="{Binding TestList}"/>
</StackPanel>
</Grid>
</Window>
Code behind: 后面的代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainViewModel();
}
}
ViewModel: 视图模型:
public class MainViewModel
{
private ObservableCollection<Test> testList;
public ObservableCollection<Test> TestList
{
get { return testList; }
set { testList = value; }
}
public MainViewModel()
{
TestList = new ObservableCollection<Test>();
TestList.Add(new Test() { Name = "a" });
TestList.Add(new Test() { Name = "b" });
TestList.Add(new Test() { Name = "c" });
}
}
Already resolved the issue. 已经解决了这个问题。 Though I don't know why binding in Array doesn't work, putting all the MenuItems into the ComboBox's Items property instead of assign ItemsSource could resolve the problem.
虽然我不知道为什么无法在Array中进行绑定,但是将所有MenuItems放入ComboBox的Items属性中,而不是AssignItemSource可以解决此问题。
Thanks all the guys replied. 谢谢所有的人回答。
Although i've just read you finally managed to solve your problem, i'm gonna explain the solution i proposed in the coments. 尽管我已经读过您终于设法解决了您的问题,但我还是要解释一下我在建议中提出的解决方案。
First, we create a ObservableCollection to feed the combobox 首先,我们创建一个ObservableCollection来填充组合框
private ObservableCollection<MenuItem> menuItems;
public ObservableCollection<MenuItem> MenuItems
{
get { return menuItems; }
set { menuItems = value; }
}
Then in the constructor, we initialize the values like this. 然后在构造函数中,我们像这样初始化值。
MenuItem a = new MenuItem();
a.Header = MainDataGrid.Items.Count;
MenuItem b = new MenuItem();
b.Header = "TESTA";
MenuItem c = new MenuItem();
c.Header = "TESTB";
MenuItems = new ObservableCollection<MenuItem>();
MenuItems.Add(a);
MenuItems.Add(b);
MenuItems.Add(c);
And finally,we bind this collection to the combobox: 最后,我们将此集合绑定到组合框:
<ComboBox ItemsSource="{Binding MenuItems}"/>
This is just to show you the idea. 这只是为了向您展示这个想法。 In your real case it maybe has to be tweaked a bit.
在您的实际情况下,可能必须对其进行一些调整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.