سؤال

I've been playing around with wpf for a few months with mixed success but this one has me stumped. I have a ListBox containing Expanders, something like this:

<ListBox>
  <Expander Header="Options">
    <StackPanel>
        <CheckBox Content="Option 1" />
        <CheckBox Content="Option 2" />
        <CheckBox Content="Option 3" />
    </StackPanel>
  </Expander>
  <Expander Header="More Options">
    <StackPanel>
        <CheckBox Content="Option 1" />
        <CheckBox Content="Option 2" />
        <CheckBox Content="Option 3" />
    </StackPanel>
  </Expander>
</ListBox>

I'm trying to figure out how to:

  1. Prevent the content portion of the Expander from changing colour when it is selected in the ListBox. I want only the Expander's header to have a blue background, not the CheckBoxes

  2. Make an Expander the selected item in the ListView when its header is clicked. At the moment you have to click outside of the Expander's header's text to have it become selected in the list view

هل كانت مفيدة؟

المحلول

The solution to your first question is simple. Just add this into the ListBox.Resources:

<ListBox.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
</ListBox.Resources>

You are free to change the colours to any that you prefer.

The solution to your second question is far from simple. I should like to say that declaring controls inside a ListBox is far from optimal. A much better solution is to create a data type class, bind a collection of these instances to the ListBox.ItemsSource property and then to create DataTemplates to define what each item should look like... that is where you should declare your Expander controls.

As at StackOverflow, we prefer one question to be asked per post, please try what I have just suggested, searching for the relevant terms online if necessary, and then come back with a new question, showing us your new code.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top