wpf - How to Bind ComboBox Inside ItemsControl with Caliburn.Micro?
问题描述
This is my project using WPF with Caliburn.Micro. In my view I have an ItemsControl that bound to a BindableCollection, in the coresponding ViewModel, of a record class (MemberVotes). The class just has two fields: MemberName and Vote. The ViewModel also has a second BindableCollection of type string (VoteOptions). The Handle method in the ViewModel loads the data into both BindableCollections. The MemberVotes is loaded from the database and the VoteOptions is loaded by adding a new string collection via code.
I am able to Display The MemberName and Vote in Textboxes with no problem, but I cannot get the ComboBox to bind the the collection of vote options. There are no error messages. The ComboBoxes are just empty.
How do I bind the ComboBoxes to the VoteOptions and then set the selected item for each ComboBox to the Vote?
Any help would be greatly appreciated.
The View (MemberVoteView):
<ItemsControl x:Name="MemberVotes">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox Text="{Binding MemberName}" Grid.Column="0" IsReadOnly="True" />
<ComboBox Grid.Column="1" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=DataContext.MemberVoteView.VoteOptions}" />
<TextBox Grid.Column="2" Text="{Binding Vote}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The ViewModel (MemberVoteViewModel):
public BindableCollection<MemberVoteModel> MemberVotes { get; set; }
public BindableCollection<string> VoteOptions { get; set; }
public void Handle()
{
MemberVotes = new BindableCollection<MemberVoteModel>();
MemberVotes .AddRange(GetVotes());
VoteOptions = new BindableCollection<string>();
VoteOptions.AddRange( new string[] { "Y", "N", "NV", "E", "O"} );
}
解决方案
I hope I understood your question correctly. Your problem lies with the following line.
<ComboBox Grid.Column="1" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=DataContext.MemberVoteView.VoteOptions}" />
The DataContext of the ItemsControl is your ViewModel. It doesn't have a property called MemberVoteView. What you needed to have instead was
<ComboBox Grid.Column="1" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=DataContext.VoteOptions}" />
To set the SelectedItem to Vote, you need to bind the SelectedItem Property of ComboBox to MemberVotes.Vote.
For Example,
<ComboBox Grid.Column="1" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=DataContext.VoteOptions}" SelectedItem="{Binding Vote}" />
推荐阅读
- php - Laravel 找不到它自动生成的数据库表
- python - 更改 matplotlib 散点图的默认边缘颜色
- c# - 如何从子查询中引用父 LINQ 查询
- excel - 试图使 IF(AND) 语句在 excel 中工作
- javascript - YouTube Iframe API - 视频有时不会通过自定义控件播放,而是会刷新视频
- python - Jupyter Notebook 滚动条不会消失?
- javascript - 是否可以从 HTML 页面运行 SQL 存储过程?
- reactjs - 如何在 ReactJS 的数据表中正确检索和显示 JSON 对象值?
- powershell - 绕过任务计划程序不起作用。有什么想法或技巧吗?
- python - Python 3.7 中的星号在这个特定场景中负责做什么?