我有ListView与分组项目。分组使用自定义GroupStyle(Expander)。我希望有一个复选框,它将展开并折叠所有组。它工作正常,直到我在组头上手动单击并展开或折叠该组。单击该特定组后停止响应复选框选择。用户手动点击组后,看起来绑定已损坏。

请告知我的错误。

非常感谢。

此致 维拉德。

<Window xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
        xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
    <Window.Resources>
        <XmlDataProvider x:Key="MyData" XPath="/Info">
            <x:XData>
                <Info xmlns="">
                    <Item Name="Item 1" Category="Cat1" />
                    <Item Name="Item 2" Category="Cat1" />
                    <Item Name="Item 3" Category="Cat2" />
                    <Item Name="Item 4" Category="Cat2" />
                    <Item Name="Item 5" Category="Cat2" />
                    <Item Name="Item 6" Category="Cat3" />
                </Info>
            </x:XData>
        </XmlDataProvider>

        <CollectionViewSource x:Key='src' Source="{Binding Source={StaticResource MyData}, XPath=Item}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="@Category" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

        <ControlTemplate x:Key="ListTemplate" TargetType="ListView">
            <ListView BorderThickness="0"
                      ItemsSource='{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ItemsSource}'
                      DisplayMemberPath="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DisplayMemberPath}">
                <ListView.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Margin" Value="0,0,0,5" />
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <Expander IsExpanded="{Binding IsChecked, ElementName=chkExpandAll, Mode=OneWay}">
                                                <Expander.Header>
                                                    <DockPanel>
                                                        <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0" Width="100" />
                                                        <TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}" />
                                                    </DockPanel>
                                                </Expander.Header>
                                                <Expander.Content>
                                                    <ItemsPresenter />
                                                </Expander.Content>
                                            </Expander>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </ListView.GroupStyle>
            </ListView>
        </ControlTemplate>
    </Window.Resources>

    <StackPanel>
        <CheckBox Name="chkExpandAll" IsChecked="True" Content="Expand All" />
        <ListView ItemsSource='{Binding Source={StaticResource src}}' DisplayMemberPath="@Name" BorderThickness="1" Template="{StaticResource ListTemplate}" />
    </StackPanel>

</Window>
有帮助吗?

解决方案

我找到了问题的解决方案。需要做的是指定Mode = TwoWay和UpdateSourceTrigger = Explicit,所以这样就不会破坏Binding,一切正常。贝娄是工作代码的一个例子。

<Window xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
        xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
    <Window.Resources>
        <XmlDataProvider x:Key="MyData" XPath="/Info">
            <x:XData>
                <Info xmlns="">
                    <Item Name="Item 1" Category="Cat1" />
                    <Item Name="Item 2" Category="Cat1" />
                    <Item Name="Item 3" Category="Cat2" />
                    <Item Name="Item 4" Category="Cat2" />
                    <Item Name="Item 5" Category="Cat2" />
                    <Item Name="Item 6" Category="Cat3" />
                </Info>
            </x:XData>
        </XmlDataProvider>

        <CollectionViewSource x:Key='src' Source="{Binding Source={StaticResource MyData}, XPath=Item}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="@Category" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

        <ControlTemplate x:Key="ListTemplate" TargetType="ListView">
            <ListView BorderThickness="0"
                      ItemsSource='{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ItemsSource}'
                      DisplayMemberPath="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DisplayMemberPath}">
                <ListView.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Margin" Value="0,0,0,5" />
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                          <StackPanel>
                                            <Expander Name="exp" IsExpanded="{Binding IsChecked, ElementName=chkExpandAll, Mode=TwoWay, UpdateSourceTrigger=Explicit}">
                                                <Expander.Header>
                                                    <DockPanel>
                                                        <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0" Width="100" />
                                                        <TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}" />
                                                    </DockPanel>
                                                </Expander.Header>
                                                <Expander.Content>
                                                    <ItemsPresenter />
                                                </Expander.Content>
                                            </Expander>
                                            </StackPanel>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </ListView.GroupStyle>
            </ListView>
        </ControlTemplate>
    </Window.Resources>

    <StackPanel>
        <CheckBox Name="chkExpandAll" Content="Expand All" />
        <ListView ItemsSource='{Binding Source={StaticResource src}}' DisplayMemberPath="@Name" BorderThickness="1" Template="{StaticResource ListTemplate}" />
    </StackPanel>

</Window>

其他提示

看起来绑定被破坏了,因为它被设置为带有复选框的OneWay,然后如果你点击任何扩展器,它将破坏绑定。将它设置为TwoWay将使它始终有效但扩展一个项目将导致它们全部扩展,这不是很有用。

我相信解决方法是不使用Bindings而是使用Storyboards。您可以触发故事板,无论当前状态如何,都会将所有扩展器设置为true / false。更新复选框可能会比较棘手,因为需要一些逻辑来查看是否所有复选框或仅检查了一些复选框。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top