1

Is it possible in XAML to

  • Define a style for panel that creates a border for each panel?
  • Change the style of an ancestor item (i.e. the border background) when an item or it's child is selected?

i.e.

<StackPanel Style="{StaticResource WidgetStyle}">
  <Label />
  <Button />
  <StackPanel>
    <ListView />
  </StackPanel>
</StackPanel>

I would like to have WidgetStyle define a (rounded) border and change the border color, e.g. if the ListView or Button is selected.

Thanks!

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
DaveO
  • 1,909
  • 4
  • 33
  • 63

1 Answers1

1

I'm afraid the answer to your first question is no! You cannot template Panel controls, they are lookless. What you could do in this. There are a few options, such as create a custom control that include a StackPanel. The answe to this question includes quite a few good ideas:

Changing WPF StackPanel template

For your second question, again no, you cannot style an ancestor based on a style change in a child by styling alone.

You could use ElementName binding to connect some properties of the elements together:

<Border Background={Binding ElementName=MyButton, Path=Tag>>
  <StackPanel Style="{StaticResource WidgetStyle}">
    <Label />
    <Button x:Name="MyButton"/>
    <StackPanel>
      <ListView />
    </StackPanel>
  </StackPanel>
</Border>

In the above, the border background is bound to the Tag property of the button 'MyButton' you can then apply a style to the button which sets the Tag property and will thus change the border background.

Community
  • 1
  • 1
ColinE
  • 68,894
  • 15
  • 164
  • 232
  • Thanks Colin! >>For your second question, again no, you cannot style an ancestor based on a style change in a child by styling alone.<< Not sure if it changes your answer but I'm looking to change style based on change in property (e.g. IsEnabled) not style. Cheers – DaveO Jan 27 '11 at 07:58