通过复选框更改一个项目控件的可见性
Change visibility of one itemscontrol via checkbox in another
我有两个 ItemsControls
具有相同的 ItemsSource
。一个对每个项目都有一些控件,另一个对每个项目都有一个 checkbox
。其中的控件是动态添加的。如何将第一个 ItemsControls
的 visibility
绑定到另一个 ItemsControls
中相应的 checkbox
?
这是行中包含多个 TextBlocks
的第一个 ItemsControl。注:我要隐藏整行控件
<ItemsControl ItemsSource="{Binding VehicleCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock />
<TextBlock />
<TextBlock />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这是第二个 ItemsControl
和 checkboxes
:
<ItemsControl ItemsSource="{Binding VehicleCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<CheckBox Content="{Binding Name}"
IsChecked="True" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
所以发生的事情是,对于 VehicleCollection
中的每个项目,为第一个 ItemsControl
添加了一个新行 textblocks
,并为第一个 checkbox
添加了一个 checkbox
第二个ItemsControl
。这些应该相互关联,例如:如果我取消选中第一个复选框,则其他 ItemsControl
的第一行应该被隐藏。
我知道如何做 booltovis 转换器,只是不确定如何关联这两个 ItemsControls。
编辑:顺便说一下,这些都在 mainwindow.xaml 中。
您应该能够通过在您的 Vehicle
class 中添加一个布尔值 属性 来管理它,我认为这是您 VehicleCollection
的基础。类似于:IsSelected
.
然后你可以修改你的XAML如下图:
<ItemsControl ItemsSource="{Binding VehicleCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Visibility="{Binding IsSelected,Converter={StaticResource boolToVisConverter}}"/>
<TextBlock Visibility="{Binding IsSelected,Converter={StaticResource boolToVisConverter}}"/>
<TextBlock Visibility="{Binding IsSelected,Converter={StaticResource boolToVisConverter}}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ItemsControl ItemsSource="{Binding VehicleCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<CheckBox Content="{Binding Name}"
IsChecked="{Binding IsSelected}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这是未经测试的 XAML,因为我只是将其输入到答案中。可能需要调整。
为了显示或隐藏第一个 ItemsControl 的项目,将 IsVisible
属性 添加到您的 Vehicle class(即 VehicleCollection
的元素类型) 并将项目 ContentPresenter
的 Visibility
绑定到 ItemContainerStyle
:
中的 IsVisible
属性
<ItemsControl ItemsSource="{Binding VehicleCollection}">
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Visibility"
Value="{Binding IsVisible,
Converter={StaticResource BooleanToVisibilityConverter}}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
...
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
在另一个ItemsControl中,将CheckBox的IsChecked
属性绑定到IsVisible
属性:
<ItemsControl ItemsSource="{Binding VehicleCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsVisible}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
当然要确保 class 车辆实现 INotifyPropertyChanged
并在 IsVisible
属性 更改时触发 PropertyChanged
事件。
我有两个 ItemsControls
具有相同的 ItemsSource
。一个对每个项目都有一些控件,另一个对每个项目都有一个 checkbox
。其中的控件是动态添加的。如何将第一个 ItemsControls
的 visibility
绑定到另一个 ItemsControls
中相应的 checkbox
?
这是行中包含多个 TextBlocks
的第一个 ItemsControl。注:我要隐藏整行控件
<ItemsControl ItemsSource="{Binding VehicleCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock />
<TextBlock />
<TextBlock />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这是第二个 ItemsControl
和 checkboxes
:
<ItemsControl ItemsSource="{Binding VehicleCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<CheckBox Content="{Binding Name}"
IsChecked="True" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
所以发生的事情是,对于 VehicleCollection
中的每个项目,为第一个 ItemsControl
添加了一个新行 textblocks
,并为第一个 checkbox
添加了一个 checkbox
第二个ItemsControl
。这些应该相互关联,例如:如果我取消选中第一个复选框,则其他 ItemsControl
的第一行应该被隐藏。
我知道如何做 booltovis 转换器,只是不确定如何关联这两个 ItemsControls。
编辑:顺便说一下,这些都在 mainwindow.xaml 中。
您应该能够通过在您的 Vehicle
class 中添加一个布尔值 属性 来管理它,我认为这是您 VehicleCollection
的基础。类似于:IsSelected
.
然后你可以修改你的XAML如下图:
<ItemsControl ItemsSource="{Binding VehicleCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Visibility="{Binding IsSelected,Converter={StaticResource boolToVisConverter}}"/>
<TextBlock Visibility="{Binding IsSelected,Converter={StaticResource boolToVisConverter}}"/>
<TextBlock Visibility="{Binding IsSelected,Converter={StaticResource boolToVisConverter}}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ItemsControl ItemsSource="{Binding VehicleCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<CheckBox Content="{Binding Name}"
IsChecked="{Binding IsSelected}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这是未经测试的 XAML,因为我只是将其输入到答案中。可能需要调整。
为了显示或隐藏第一个 ItemsControl 的项目,将 IsVisible
属性 添加到您的 Vehicle class(即 VehicleCollection
的元素类型) 并将项目 ContentPresenter
的 Visibility
绑定到 ItemContainerStyle
:
IsVisible
属性
<ItemsControl ItemsSource="{Binding VehicleCollection}">
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Visibility"
Value="{Binding IsVisible,
Converter={StaticResource BooleanToVisibilityConverter}}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
...
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
在另一个ItemsControl中,将CheckBox的IsChecked
属性绑定到IsVisible
属性:
<ItemsControl ItemsSource="{Binding VehicleCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsVisible}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
当然要确保 class 车辆实现 INotifyPropertyChanged
并在 IsVisible
属性 更改时触发 PropertyChanged
事件。