繁体   English   中英

如何使用Caliburn.Micro在ItemsControl内绑定ComboBox?

[英]How to Bind ComboBox Inside ItemsControl with Caliburn.Micro?

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

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

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

任何帮助将不胜感激。

视图(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):

    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"} );
    }

希望我能正确理解您的问题。 您的问题在于以下几行。

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

ItemsControl的DataContext是您的ViewModel。 它没有名为MemberVoteView的属性。 您需要拥有的是

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

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

例如,

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

Caliburn.Micro与约定有关。 如果将ComboBoxName设置为“ VoteOptions”,并将VoteOptions属性移动到MemberVoteModel类,并且还向该类添加SelectedVoteOption属性(具有此确切名称),则应该可以进行以下操作:

<DataTemplate>
    <Grid cal:Bind.Model="{Binding}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="4*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <TextBox x:Name="MemberName" Grid.Column="0" IsReadOnly="True" />

        <ComboBox x:Name="VoteOptions" />

        <TextBox Grid.Column="2" x:Name="SelectedVoteOption" />
    </Grid>
</DataTemplate>

您还应该考虑将MemberVoteModel重命名为MemberVoteViewModel (您必须使用其他名称或重命名当前视图模型),并为视图定义一个单独的名称。 这将是Caliburn.Micro的设置方式。

如果决定将VoteOptions属性保留在当前视图模型类中,则可以使用如下内置机制将其绑定:

<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=DataContext.VoteOptions}"
          SelectedItem="{Binding SelectedVoteOption}"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM