【发布时间】:2012-03-05 21:13:39
【问题描述】:
我正在使用 C# 和 WPF,使用 MVVM 创建一个 Windows 应用程序。我正在尝试创建一个可折叠的项目控件,以便显示集合中的项目。展开每个项目时,应显示一个包含项目属性的组框。我在 UserControl 中有以下内容:
<UserControl.Resources>
<SolidColorBrush x:Key="GlyphBrush" Color="#444" />
<ControlTemplate x:Key="toggleButtonTemplate" TargetType="ToggleButton">
<Grid
Width="15"
Height="13"
Background="Transparent">
<Path x:Name="ExpandPath"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Margin="1,1,1,1"
Fill="{StaticResource GlyphBrush}"
Data="M 4 0 L 8 4 L 4 8 Z"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked"
Value="True">
<Setter Property="Data"
TargetName="ExpandPath"
Value="M 0 4 L 8 4 L 4 8 Z"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style x:Key="toggleButtonStyle" TargetType="ToggleButton">
<Setter Property="Template" Value="{StaticResource toggleButtonTemplate}" />
</Style>
<BooleanToVisibilityConverter x:Key="VisibilityOfBool" />
<Style x:Key="CollapsibleListStyle" TargetType="{x:Type ItemsControl}">
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style>
<Setter Property="Control.Margin" Value="5" />
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="0" Focusable="False">
</ContentPresenter>
<ToggleButton x:Name="toggleButton"
Grid.Column="1" IsChecked="False" Margin="3.5"
Style="{StaticResource toggleButtonStyle}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<WrapPanel>
<ItemsControl Name="itemsList"
Style="{StaticResource CollapsibleListStyle}"
ItemsSource="{Binding ViewModel.Items}"
Margin="0,0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" Grid.Column="0"
FontWeight="DemiBold" VerticalAlignment="Center"/>
<GroupBox Header="Properties" Grid.Column="1" Margin="5"
Visibility="{Binding ElementName=toggleButton,
Path=IsChecked, Converter={StaticResource VisibilityOfBool}}">
<WrapPanel HorizontalAlignment="Center">
<StackPanel Orientation="Horizontal" Margin="5">
<TextBlock Text="Code: "/>
<TextBlock Text="{Binding ItemCode}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="5">
<TextBlock Text="Key: "/>
<TextBlock Text="{Binding ItemKey}"/>
</StackPanel>
</WrapPanel>
</GroupBox>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</WrapPanel>
这会在运行时产生以下错误:
System.Windows.Data 错误:4:找不到绑定源 参考“元素名称=切换按钮”。 绑定表达式:路径=IsChecked;数据项=空;目标元素 是'组框'(名称='');目标属性是“可见性”(类型 '可见性')
所以不显示切换按钮。 在我的应用程序的另一个地方,我使用了上面的方法,但用 ListBox 替换了 ItemsControl,以获得一个可折叠的列表框,并且代码可以正常工作。 但是,这里我不想要选择功能。
有人可以帮忙吗?
谢谢你,布赖恩
【问题讨论】:
-
我没有看到任何绑定到
toggleButtonModel... -
是的,应该是
toggleButton。复制了错误的输出行。我已经编辑了这个问题。绑定应该在 Groupbox 的 Visibility 属性中。
标签: .net wpf xaml itemscontrol togglebutton