如何使用Caliburn.Micro在ItemsControl内绑定ComboBox? [英] How to Bind ComboBox Inside ItemsControl with Caliburn.Micro?

查看:218
本文介绍了如何使用Caliburn.Micro在ItemsControl内绑定ComboBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用带有Caliburn.Micro的WPF的项目.在我看来,我有一个ItemsControl绑定到记录类(MemberVotes)的核心响应ViewModel中的BindableCollection.该类只有两个字段:MemberName和Vote. ViewModel还具有第二个类型为String(VoteOptions)的BindableCollection. ViewModel中的Handle方法将数据加载到两个BindableCollections中.从数据库中加载MemberVotes,并通过代码添加新的字符串集合来加载VoteOptions.

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.

我能够在文本框中显示MemberName和Vote没问题,但是我无法使ComboBox绑定投票选项集合.没有错误消息.组合框只是空的.

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.

如何将ComboBox绑定到VoteOptions,然后将每个ComboBox的选定项设置为Vote?

How do I bind the ComboBoxes to the VoteOptions and then set the selected item for each ComboBox to the Vote?

任何帮助将不胜感激.

视图(MemberVoteView):

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>

ViewModel(MemberVoteViewModel):

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}" />

ItemsControl的DataContext是您的ViewModel.它没有名为MemberVoteView的属性.相反,您需要拥有的是

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}" />

要将SelectedItem设置为Vote,需要将ComboBox的SelectedItem属性绑定到MemberVotes.Vote.

To set the SelectedItem to Vote, you need to bind the SelectedItem Property of ComboBox to MemberVotes.Vote.

例如,

<ComboBox Grid.Column="1" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=DataContext.VoteOptions}" SelectedItem="{Binding Vote}" />

这篇关于如何使用Caliburn.Micro在ItemsControl内绑定ComboBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆