7

たとえば、ListBoxItems のアイテム間にセパレーターを組み込む必要があります。たとえば、アイテム ソースの一部のアイテムがセパレーターの下に配置され、一部がセパレーターの上に配置されます。

例えば ​​:

セパレーター付きリストボックス

上記は、 ListBox の ControlTemplate を変更することによって行われます。

 <ScrollViewer>
     <StackPanel>
         <ItemsPresenter />                                        
         <Separator  BorderBrush="Red"  />
         <ListBoxItem Content=".." ContentTemplate="..."  x:Key="helpItem"/>                                    
     </StackPanel>
 </ScrollViewer>

問題は、ItemsSource の一部ではないため、「helpItem」が選択されないことです。

とりあえず選択できれば十分

1)最初の質問は、このアイテムを ItemsSource に関連付ける方法、または選択可能にする方法です。

さらに将来的には、リストボックスの下半分に配置されるアイテムを増やしたくないかもしれません

2) 論理的な場所で ItemsPresenter を分割するように、アイテム間の特定の場所に Separator を物理的に配置するにはどうすればよいですか?

4

1 に答える 1

9

複数のListBoxコントロールの代わりに、必要なセパレーターの数に基づいてコレクションを「n」個の小さなグループに分割できる場合は、それらをすべて aCompositeCollectionを介して同じグループにまとめることができますListBox

たとえば、私が持っているとしましょう:

public partial class MainWindow : Window {
  public List<string> CollA { get; set; }
  public List<string> CollB { get; set; }
  public MainWindow() {
    InitializeComponent();

    CollA = new List<string> {"A", "B", "C"};

    CollB = new List<string> {"D", "E", "F"};

    DataContext = this;
  }
}

CollAと の間にセパレータが必要な場合、xaml はCollB次のようになります。

<ListBox>
  <ListBox.Resources>
    <CollectionViewSource x:Key="CollectionOne"
                          Source="{Binding CollA}" />
    <CollectionViewSource x:Key="CollectionTwo"
                          Source="{Binding CollB}" />
  </ListBox.Resources>
  <ListBox.ItemsSource>
    <CompositeCollection>
      <CollectionContainer Collection="{Binding Source={StaticResource CollectionOne}}" />
      <ListBoxItem HorizontalContentAlignment="Stretch"
                    IsEnabled="False"
                    IsHitTestVisible="False">
        <Rectangle Height="2"
                    Fill="Gray" />
      </ListBoxItem>
      <CollectionContainer Collection="{Binding Source={StaticResource CollectionTwo}}" />
    </CompositeCollection>
  </ListBox.ItemsSource>
</ListBox>

生成する必要があります:

ここに画像の説明を入力

アイテムが機能するようになり、SelectedItemout をバインドして必要に応じて操作できるようになりました。また、SelectedItem を source-collection に対してチェックすることで、現在選択されているアイテムがどのソース リストに属しているかを検出できます。

于 2013-08-21T13:11:30.820 に答える