2

グループスタイルのリストビューがあり、エキスパンダー (以下のコード) を定義しています。適切なエキスパンダーに追加されるリストビューにプログラムで多くの項目を追加し、新しい項目に対してエキスパンダーが存在しない場合は動的に作成されます。

<ListView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Expander IsExpanded="True" >
                                            <Expander.Header>
                                                <DockPanel>
                                                    <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0" Width="100"/>
                                                </DockPanel>
                                            </Expander.Header>
                                            <Expander.Content>
                                                <ItemsPresenter />
                                            </Expander.Content>
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </ListView.GroupStyle>

したがって、必要なことは、新しいアイテムが追加されたときに、フォーカスをそのアイテムに向け、エキスパンダーを展開し、他のすべてを折りたたむことです...

4

2 に答える 2

7

前の回答を拡張したかった-MVVMシナリオでこれを使用しており、上記のビリーと同じエラーが発生していました

「「Binding」は、タイプ「Binding」の「ConverterParameter」プロパティには設定できません。「Binding」は、DependencyObject の DependencyProperty にのみ設定できます。

最終的に、コンバーターと XAML を次のように変更しました (DataContext.CurrentItem は、ビュー モデルで現在の項目を参照するために必要なもののプレースホルダーです)。

public class MultiBindingItemsEqualConverter : IMultiValueConverter
{
    #region IMultiValueConverter Members

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        bool valuesEquals = values[0].Equals(values[1]);
        return valuesEquals;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

<Expander ...>
    <Expander.IsExpanded>
        <MultiBinding
            Mode="OneWay"
            Converter="{StaticResource MultiBindingItemsEqualConv}">
            <Binding
                RelativeSource="{RelativeSource FindAncestor, AncestorType=ListView, AncestorLevel=1}"
                Path="SelectedItem"></Binding>
            <Binding
                RelativeSource="{RelativeSource FindAncestor, AncestorType=ListView, AncestorLevel=1}"
                Path="DataContext.CurrentItem"></Binding>
        </MultiBinding>
    </Expander.IsExpanded>
</Expander>
于 2012-10-15T15:37:34.053 に答える
2

バインドを使用して、リストの SelectedItem がバインド先のグループの一部であるかどうかを確認します。

<Expander IsExpanded="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListView, AncestorLevel=1}, Path=SelectedItem, Converter={StaticResource yourConverter}, ConverterParameter={Binding}}" >

ビューモデルにバインドされたコンバーター パラメーターを使用して IsExpanded をリストの SelectedItem にバインドし、コンバーターに引数が一致するかどうかを確認させるだけです。

コンバーターは単に true または false を返します

public class yourConvertor : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((GroupItem)parameter).Contains(value);
    }
 }
于 2012-09-07T13:59:42.817 に答える