kw1jybo
kw1jybo

Reputation: 152

Creating a style based on another which is using a data template

I have a style for a ListBox using TextBoxes as defined below:

       <Style x:Key="MyListBoxStyle" TargetType="ListBox">
        <Setter Property="Background" Value="LightGray"/>
        <Setter Property="BorderThickness" Value="2"/>
        <Setter Property="BorderBrush" Value="Black"/>
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBox Text="{Binding Mode=OneWay}" Background="LightGray"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

I use this style for a number of ListBox display readonly data.

What I also have is a group of ListBoxes in which I would like to be able to click on one of the TextBoxes in them and then perform some action. So what I want to do is attach a Click event on the TextBox. Ideally I'd like to leverage the above style without having to retype it all and just add a new style which added the event binding. How would yo go about this?

Upvotes: 0

Views: 44

Answers (1)

Mr. Squirrel.Downy
Mr. Squirrel.Downy

Reputation: 1177

Style.BaseOn

<Style x:Key="MyListBoxStyle" TargetType="ListBox">
    <Setter Property="Background" Value="LightGray"/>
    <Setter Property="BorderThickness" Value="2"/>
    <Setter Property="BorderBrush" Value="Black"/>
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBox Text="{Binding Mode=OneWay}" Background="LightGray"/>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="MyListBoxStyle2" BasedOn="{StaticResource MyListBoxStyle}" TargetType="ListBox">
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBox Text="{Binding Mode=OneWay}" Background="LightGray" GotFocus="TextBox_GotFocus"/>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

Upvotes: 1

Related Questions