带有子集合的嵌套 ItemControl 上的 WPF 绑定 [英] WPF Binding on Nested ItemControls with Sub Collection

查看:73
本文介绍了带有子集合的嵌套 ItemControl 上的 WPF 绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 xaml:

<ItemsControl ItemsSource="{Binding ResearchLanguageViewModel.Filters, Mode=OneWay}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Path=Type}"></TextBlock>
                <TextBox Text="Search...."></TextBox>
                <ItemsControl>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <CheckBox Content="{Binding Path=Values}"></CheckBox>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

具有以下对象/集合:

public class FilterViewModel
{
    public string Type { get; set; }
    public ObservableCollection<string> Values { get; set; }
}

public class ResearchLanguageViewModel
{
    public int FirmCount { get; set; }
    public ObservableCollection<FilterViewModel> Filters { get; set; } 
}

我正在尝试绑定 Filters 属性并且 Type 属性很好.但是,我无法将字符串的 Values 集合显示为一组复选框.不知道我在这里做错了什么...

I'm trying to bind on the Filters property and the Type property comes out fine. However, I am having trouble getting the Values collection of strings to show as a group of checkboxes. Not sure what I'm doing wrong here...

推荐答案

您需要将 Values 绑定到 ItemsControl 而不是 Checkbox,

You will want to bind Values to the ItemsControl not the Checkbox,

 <StackPanel>
    <TextBlock Text="{Binding Path=Type}"></TextBlock>
    <TextBox Text="Search...."></TextBox>
    <ItemsControl ItemsSource="{Binding Values}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding}"></CheckBox>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</StackPanel>

这篇关于带有子集合的嵌套 ItemControl 上的 WPF 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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