[英]WPF - Conditional binding in ItemsControl
I'm very new to WPF, and am just getting started with data binding.我对 WPF 很陌生,我刚刚开始使用数据绑定。 What I'd like to do is generate a list of checkboxes based on a list in my view model.
我想做的是根据我的视图模型中的列表生成一个复选框列表。 The XAML I have at the moment is:
我目前拥有的 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>
This works correctly, and generates a checkbox for every item in TestList
.这可以正常工作,并为
TestList
每个项目生成一个复选框。 What I'd like to do is only generate checkboxes for items where the condition TestList[i].Type == "Mode"
is true.我想要做的只是为条件
TestList[i].Type == "Mode"
为真的项目生成复选框。 I believe that I may need to use a <DataTrigger>
element, but I don't know the details of how to do this.我相信我可能需要使用
<DataTrigger>
元素,但我不知道如何执行此操作的详细信息。
[EDIT] Just to clarify, each element of TestList
has Name
, Enabled
, and Type
properties. [编辑] 只是为了澄清,
TestList
每个元素都有Name
、 Enabled
和Type
属性。
There are several ways to do this.有几种方法可以做到这一点。 However, the simplest static approach would be to just filter it at your ViewModel
但是,最简单的静态方法是在您的ViewModel 中对其进行过滤
Filtered = new ObservableCollection(TestList.Where(x => x.Type == "Mode"));
...
<ItemsControl ItemsSource="{Binding Path=Filtered , UpdateSourceTrigger=PropertyChanged}">
Note : There are fancier more dynamic ways to achieve this, though this might help you out注意:有更高级的更动态的方法来实现这一点,尽管这可能会帮助你
As I guess that you want to make the checkbox appear if TestList.Type changes, I would suggest make a Converter and Bind it to the CheckBox Visibility.我猜你想让复选框在 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;
}
}
And then in the dictionary add the reference to your namespace然后在字典中添加对您的命名空间的引用
xmlns:converters="clr-namespace:Projct.Converters;
and in the resources dictionary并在资源字典中
<converters:CheckBoxVisibilityConverter x:Key="CheckBoxConverter"/>
Finally in the xaml最后在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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.