我有这个树视图的主要目的是在建筑物中显示打开和关闭的窗口。现在这些课程是" floor",其中包含一系列房间,"房间"有一个窗口列表和" roomwindow"类。
我希望通过根据" isOpen"检查相应的单选按钮来显示打开的窗口,关闭的窗口或所有窗口。 roomwindow类的布尔值。
有没有办法暂时隐藏或删除"窗口" treeview项基于房间窗口的布尔值? 或者是唯一可能遍历房间窗口列表并删除窗口?
WindowCheck.xaml.cs
<Window x:Class="WpfApplication3.WindowCheck"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication3"
Title="WindowCheck" Height="350" Width="525" x:Name= "WindowCheckWindow">
<Grid Margin="104,113,60,0">
<Grid.Resources>
<local:BoolToVisibleOrHidden x:Key="BoolToVisConverter" Reverse="False"/>
<local:BoolToVisibleOrHidden x:Key="BoolToVisConverterReverse" Reverse="True"/>
</Grid.Resources>
<RadioButton x:Name="rdbtn_open" Content="Open windows" HorizontalAlignment="Left" Height="43" Margin="99,-79,0,0" VerticalAlignment="Top" Width="118"/>
<RadioButton x:Name="rdbtn_closed" Content="Closed windows" HorizontalAlignment="Left" Height="43" Margin="256,-79,-30,0" VerticalAlignment="Top" Width="127" />
<RadioButton x:Name="rdbtn_all" Content="All windows" HorizontalAlignment="Left" Margin="-55,-79,0,0" VerticalAlignment="Top" Height="33" Width="96"/>
<TreeView ItemsSource="{Binding FloorList, ElementName=WindowCheckWindow, Mode=TwoWay}">
<!--RoomTemplate-->
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding RoomList}">
<TextBlock Text="{Binding FloorId}"/>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding RoomWindowList}">
<TextBlock Text="{Binding RoomId}" />
<!--Window-->
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate >
<TextBlock Text="{Binding WindowId}" >
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=rdbtn_closed}" Value="True">
<Setter Property= "TextBlock.Visibility" Value="{Binding Path= IsOpen, Converter={StaticResource BoolToVisConverter}}" />
</DataTrigger>
<DataTrigger Binding="{Binding IsChecked, ElementName=rdbtn_open}" Value="True">
<Setter Property="TextBlock.Visibility" Value="{Binding Path=IsOpen,Converter={StaticResource BoolToVisConverterReverse}}" />
</DataTrigger>
<DataTrigger Binding="{Binding IsChecked, ElementName=rdbtn_all}" Value="True">
<Setter Property="TextBlock.Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Style.Triggers>
<DataTrigger Binding ="{ Binding IsOpen}" Value="True">
<Setter Property="Background" Value="Salmon"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsOpen}" Value="False">
<Setter Property="Background" Value="GreenYellow">
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
</Grid>
MainWindow.xaml.cs,其中初始化了测试版:
public partial class MainWindow : Window
{
private Floor floor;
public MainWindow()
{
InitializeComponent();
InitializeBuilding();
}
public void InitializeBuilding()
{
floor = new Floor("FirstFloor");
List<RoomWindow> bathroomWindows = new List<RoomWindow>();
bathroomWindows.Add(new RoomWindow("Bathroom window 1", false));
//etc.
List<RoomWindow> livingroomWindows = new List<RoomWindow>();
livingroomWindows.Add(new RoomWindow("Livingroom window 1", false));
//etc.
Room livingRoom = new Room("Livingroom",livingroomWindows,floor);
Room bathRoom = new Room("Bathroom",bathroomWindows,floor);
floor.addRooms(livingRoom);
floor.addRooms(bathRoom);
}
//...
答案 0 :(得分:0)
我添加了一个自定义的BooleanToVisibilityConverter,它根据&#34; isOpen&#34;的布尔值隐藏窗口。这似乎只有一个问题可以正常工作。现在,如果某些窗口项目已折叠,则该项目仍然存在空白区域。