1

グループ化されたアイテムを含む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>
4

2 に答える 2

2

私はその問題の解決策を見つけました。実行する必要があるのは、Mode=TwoWayおよびUpdateSourceTrigger=Explicitを指定することです。これにより、バインディングが壊れることはなく、すべてが正常に機能します。以下は、動作するコードの例です。

<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>
于 2008-10-23T15:11:34.253 に答える
0

チェックボックスで OneWay に設定されているため、バインディングが壊れているように見えます。その後、いずれかのエキスパンダーをクリックすると、バインディングが壊れます。TwoWay に設定すると常に機能しますが、1 つのアイテムを展開するとすべてが展開されるため、あまり役に立ちません。

これに対する解決策は、バインディングを使用せず、代わりにストーリーボードを使用することだと思います。現在の状態に関係なく、すべてのエキスパンダーを true/false に設定するストーリーボードを起動できます。ただし、すべてのチェックボックスがチェックされているか、一部のみがチェックされているかを確認するために必要なロジックがあるため、チェックボックスを更新するのは難しいかもしれません。

于 2008-10-22T21:18:23.107 に答える