【问题标题】:Collapsible items control in wpf - "System.Windows.Data Error: 4"wpf 中的可折叠项控件-“System.Windows.Data 错误:4”
【发布时间】: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


【解决方案1】:

如果您知道TargetType,您应该始终指定它,您的样式不能正确应用,因为您通过不设置它使其通用,同时设置容器上不存在的属性,因此被忽略。

TargetType 更改为ContentPresenter,您将无法再设置Template

<ItemsControl.ItemContainerStyle>
    <Style TargetType="ContentPresenter">
        <Setter Property="Template"> <!-- will throw an error -->

您需要将所有内容移至ItemTemplate,因为没有要设置的ControlTemplate

【讨论】:

  • 但它确实奏效了。我认为它们在同一个范围内,这不是ContentPresenter 的目的吗?我不确定的更改位于&lt;ControlTemplate TargetType="ContentControl"&gt; 行。在使用 ListBox 时,它曾经是 &lt;ControlTemplate TargetType={x:Type ListBoxItem}&gt;
  • @BrianDay:ItemsControl 中的容器应该是ContentPresenter,而ListBox 中的容器显然是ListBoxItem。您甚至不能更改模板,因为 ContentPresenters 没有这样的属性,因此不会应用模板,因此找不到元素。
  • 我明白了。最终使用了没有复选框功能的ListBox。无论如何,非常感谢。
猜你喜欢
  • 1970-01-01
  • 2012-11-10
  • 1970-01-01
  • 2014-05-23
  • 2017-01-31
  • 2018-04-01
  • 2018-08-09
  • 2011-04-26
  • 2019-02-16
相关资源
最近更新 更多