Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF-ItemsControl中的条件绑定_C#_Wpf - Fatal编程技术网

C# WPF-ItemsControl中的条件绑定

C# WPF-ItemsControl中的条件绑定,c#,wpf,C#,Wpf,我对WPF非常陌生,我刚刚开始使用数据绑定。我想做的是根据视图模型中的列表生成复选框列表。我目前拥有的XAML是: <ItemsControl ItemsSource="{Binding Path=TestList, UpdateSourceTrigger=PropertyChanged}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPane

我对WPF非常陌生,我刚刚开始使用数据绑定。我想做的是根据视图模型中的列表生成复选框列表。我目前拥有的XAML是:

<ItemsControl ItemsSource="{Binding Path=TestList, UpdateSourceTrigger=PropertyChanged}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding Path=Name}" IsChecked="{Binding Path=Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="10,5,10,5" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

这可以正常工作,并为
TestList
中的每个项目生成一个复选框。我只想为条件
TestList[I].Type==“Mode”
为true的项目生成复选框。我相信我可能需要使用
元素,但我不知道如何做的细节


[编辑]只是为了澄清,
TestList
的每个元素都有
名称
启用
,以及
类型
属性。

有几种方法可以做到这一点。但是,最简单的静态方法是在ViewModel中对其进行过滤

Filtered=newobserveCollection(TestList.Where(x=>x.Type==“Mode”);
...

注意:有更为奇特的动态方法来实现这一点,尽管这可能会帮助您解决问题

,因为我猜您希望在TestList.Type更改时显示复选框。我建议制作一个转换器并将其绑定到复选框可见性

public sealed class CheckBoxVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null || parameter == null)
            return Visibility.Visible;

        var type = (string)value;
        var condition  = (string)parameter;
        return type.Equals(condition) ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}
然后在字典中添加对您的命名空间的引用

xmlns:converters="clr-namespace:Projct.Converters;
在资源字典里

 <converters:CheckBoxVisibilityConverter x:Key="CheckBoxConverter"/>

最后在xaml中

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <CheckBox
            Margin="10,5,10,5"
            Content="{Binding Path=Name}"
            IsChecked="{Binding Path=Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            Visibility="{Binding Path=Type, Converter={StaticResource CheckBoxConverter}, ConverterParameter=Mode}" />    
    </DataTemplate>
</ItemsControl.ItemTemplate> 


这将在短期内起作用,但我需要为每个
类型
值设置一个
ItemControl
,并且不同
类型
值的数量可能会发生变化。动态执行会更好。也许你应该查看
CollectionViewSource
,甚至是
CompositeCollection
。我认为这可能是一种方法,但是,当我编译上述代码时,类型“System.Windows.StaticResourceExtension”的未知属性“ConverterParameter”出现错误。@Tevildo请确保将xmlns引用添加到转换器所在的命名空间中。是-如果我删除命名空间引用,我发现
CheckBoxVisibilityConverter未找到
@Tevildo好的,我在代码的可见性部分输入了一个错误对不起。现在应该可以了。这就解决了,谢谢。我在可见性参数中尝试了各种括号和大括号的组合,但没有找到正确的组合。
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <CheckBox
            Margin="10,5,10,5"
            Content="{Binding Path=Name}"
            IsChecked="{Binding Path=Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            Visibility="{Binding Path=Type, Converter={StaticResource CheckBoxConverter}, ConverterParameter=Mode}" />    
    </DataTemplate>
</ItemsControl.ItemTemplate>