shenku
shenku

Reputation: 12448

How to have multiple window resource styles for same element type in WPF

I have an element in my window, as below:

<Grid>        
    <DockPanel LastChildFill="True">
        <Label Name="StatisticsLabel" DockPanel.Dock="Bottom"></Label>
        <RichTextBox Style="{StaticResource FocusMode}" Name="RichTextBox1"  />            
    </DockPanel>
</Grid>

I would like to swith between two styles at runtime depending of the state I need the control to be in.

I had assumed I could use the following code:

<Window.Resources>
    <Style x:Name="FocusMode" TargetType="RichTextBox">
        <Setter Property="VerticalScrollBarVisibility" Value="Disabled"></Setter>
    </Style>
    <Style x:Name="NormalMode" TargetType="RichTextBox">
        <Setter Property="VerticalScrollBarVisibility" Value="Auto"></Setter>            
    </Style>
</Window.Resources>

Of course this isn't working.

Why does WPF not support multiple styles per element? Seems like a pretty basic requirement?

Otherwise, how do I achieve this?

Upvotes: 1

Views: 1843

Answers (2)

Erik Dietrich
Erik Dietrich

Reputation: 6090

I'd have a look at style triggers. You can probably get a good start on the subject from this post: How to make Style.Triggers trigger a different named style to be applied

Upvotes: 0

shenku
shenku

Reputation: 12448

Sorry figured it out, instead of x:Name use x:Key as below:

<Window.Resources>
<Style x:Key="FocusMode" TargetType="RichTextBox">
    <Setter Property="VerticalScrollBarVisibility" Value="Disabled"></Setter>
</Style>
<Style x:Key="NormalMode" TargetType="RichTextBox">
    <Setter Property="VerticalScrollBarVisibility" Value="Auto"></Setter>            
</Style>

Upvotes: 2

Related Questions