1

私は奇妙な状況にいます。特定のカテゴリでグループ化されたアイテムのリストを生成しています。私のビュー モデルでは、各リスト アイテムを格納するために作成したクラスを表す のインスタンスReadOnlyDictionary<string, List<CustomObject>>にアイテムを格納しています。CustomObject文字列はカテゴリです。私の見解では、StackPanelその<ItemsControl>中に があります。項目コントロールには、ItemTemplate次のような種類があります。

<DataTemplate x:Key="DataTemplateName">
    <StackPanel>
        <Separator />
        <TextBlock Text="{Binding Key}" />
        <ItemsControl ItemsSource="{Binding Value}" />
    </StackPanel>
</DataTemplate>

上記のバインディングはうまく機能します。問題は、最初の項目の上にセパレーターを置きたくないということです。だから、最初のアイテムには別のスタイルが必要だと思います.

を使用してみましたItemTemplateSelectorが、問題は、現在のアイテムにしかアクセスできないため、最初の要素にあるかどうかを知る方法がないことです。私も次のようなことをしてみました

<Separator 
    Visibility={Binding ShowSeparator, RelativeSource={RelativeSource AncestorType={x:Type CustomObject}}}" />

...ここで、ShowCategories は CustomObject クラスの依存関係プロパティであり、ReadOnlyDictionary インスタンスを見て、区切り記号を表示するかどうかを示します。しかし、これを行うと ShowCategories にアクセスすることはありません。あったとしても、どのアイテムがそれを呼び出しているかを知る方法はないと思います。

そう。私は何をしますか?

4

2 に答える 2

0

VM に新しいプロパティを追加したり、ItemTemplateSelector.

これは単なるビュー関連であり、「テスト」が必要なロジックとは関係がないため、これを xaml だけで保持することをお勧めします

したがって、xaml ソリューションは、ItemsControl.AlternationIndexを使用することです。

何かのようなもの:

<ListBox AlternationCount="{Binding RelativeSource={RelativeSource Self}, Path=ItemsSource.Count, Mode=OneWay}">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel>
        <Separator x:Name="seperator" />
        <TextBlock Text="{Binding Key}" />
        <ItemsControl ItemsSource="{Binding Value}" />
      </StackPanel>
      <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Path=(ListBox.AlternationIndex),
                                        RelativeSource={RelativeSource FindAncestor,
                                                                      AncestorType={x:Type ListBoxItem}}}"
                      Value="0">
          <Setter TargetName="seperator"
                  Property="Visibility"
                  Value="Hidden" />
        </DataTrigger>
      </DataTemplate.Triggers>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

このスニペットの 2 つの重要な部分。

  • に をと同じように割り当てて、AlternationCountそれぞれに一意の を生成します。ListBoxItemSource.CountAlternationIndexListBoxItem
  • DataTemplate.DataTrigger現在のアイテムをチェックして、それAlternationIndexが 0かどうかを確認し、0 の場合は非表示にしseperatorます。
于 2013-07-06T17:52:50.533 に答える
0

私はMVVMアプローチを採用します:

  1. CustomObjectViewModelを、CustomObjectの代わりに Dictionary に保持し、CustomObject を Model として使用します。

  2. CustomObjectViewModel では、 CustomObjectService (または簡単にするために Dictionary への参照) にアクセスできます。

  3. 上記のサービスで、次のようなメソッドを使用します。

    Public Boolean IsCustomObjectFirst(CustomObjectViewModel vm){..}

    それは明らかに、指定されたアイテムの位置をチェックします。

  4. CustomObjectViewModelにプロパティShouldDisplaySeperatorを設定するだけで、値を取得できますCustomObjectService.IsCustomObjectFirst(this)

  5. 次のようなものを使用して、ビューの定義で使用するだけです。

    ビューの定義:

    <DataTemplate x:Key="DataTemplateName">
        <StackPanel>
            <Separator Visibilty="{Binding ShouldDisplaySeperator,Converter={StaticResource BooleanToVisibilityConverter}}" />
            <TextBlock Text="{Binding Model.Key}" />
            <ItemsControl ItemsSource="{Binding Model.Value}" />
        </StackPanel>
    </DataTemplate>
    
于 2013-07-06T17:46:00.253 に答える