.net wpf中的可折叠项控件-“;System.Windows.Data错误:4“;

.net wpf中的可折叠项控件-“;System.Windows.Data错误:4“;,.net,wpf,xaml,itemscontrol,togglebutton,.net,Wpf,Xaml,Itemscontrol,Togglebutton,我正在使用C#和WPF,使用MVVM创建一个Windows应用程序。我试图创建一个可折叠的items控件,以便显示集合中的项目。展开每个项目时,应显示包含项目属性的groupbox。我在UserControl中有以下内容: <UserControl.Resources> <SolidColorBrush x:Key="GlyphBrush" Color="#444" /> <ControlTemplate x:Key="toggleButtonTemplate" T

我正在使用C#和WPF,使用MVVM创建一个Windows应用程序。我试图创建一个可折叠的items控件,以便显示集合中的项目。展开每个项目时,应显示包含项目属性的groupbox。我在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:找不到与绑定的源 参考'ElementName=toggleButton'。 BindingExpression:Path=IsChecked;DataItem=null;目标元素 是“GroupBox”(名称=“”);目标属性为“可见性”(类型 "能见度")

因此,不会显示切换按钮。 在我的应用程序中的另一个地方,我使用了上面的方法,但用一个列表框替换了ItemsControl,以获得一个可折叠的列表框,代码按其应该的方式工作。 但是,这里我不想要选择功能

有人能帮忙吗


谢谢您,Brian

您应该始终指定
TargetType
,如果您知道它,您的样式不会正确应用,因为您没有设置它,同时设置了容器上不存在的属性,因此会忽略这些属性,从而使其成为通用样式

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

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


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

您应该始终指定
TargetType
,如果您知道,您的样式无法正确应用,因为您通过不设置样式而使其成为通用样式,同时设置了容器上不存在的属性,因此这些属性将被忽略

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

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


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

我没有看到任何绑定到
toggleButtonModel
…是的,应该是
toggleButton
。复制了错误的输出行。我已经编辑了这个问题。绑定应该在Groupbox的Visibility属性中。我没有看到任何绑定到
toggleButtonModel
…是的,应该是
toggleButton
。复制了错误的输出行。我已经编辑了这个问题。绑定应该在Groupbox的Visibility属性中。但它确实有效。我认为它们在同一范围内,这不是
ContentPresenter
的目的吗?我不确定的更改在第
行。使用
ListBox
@BrianDay时通常是
:在
ItemsControl
中的容器应该是
ContentPresenter
,而在
ListBox
中它显然是
ListBoxItem
。您甚至不能更改模板,因为
ContentPresenters
没有此类属性,因此模板将不会应用,因此找不到元素。我明白了。最后使用了一个没有复选框功能的
列表框。不管怎样,非常感谢。但它确实起了作用。我认为它们在同一范围内,这不是
ContentPresenter
的目的吗?我不确定的更改在第
行。使用
ListBox
@BrianDay时通常是
:在
ItemsControl
中的容器应该是
ContentPresenter
,而在
ListBox
中它显然是
ListBoxItem
。您甚至不能更改模板,因为
ContentPresenters
没有此类属性,因此模板将不会应用,因此找不到元素。我明白了。最后使用了一个没有复选框功能的
列表框。无论如何,非常感谢。