2

I have these styles defined:

<Style x:Key="ImgButton" TargetType="Button">
    <Setter Property="Background" Value="Transparent"></Setter>
    <Setter Property="Height" Value="30"></Setter>
</Style>
<Style x:Key="ImgButtonWithText" TargetType="{x:Type Button}" BasedOn="{StaticResource ImgButton}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Name="ButtonImage" Stretch="Uniform" Height="20" Width="20" Source="">
                    </Image>
                    <TextBlock Name="ButtonText" Margin="4,0" VerticalAlignment="Center" Text=""></TextBlock>
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

I want to create another style, LoginButtonWithText (shown below), that is based on ImgButtonWithText, and which sets the value of the Source property of the ButtonImage control in the ImgButtonWithText style. I want to do the same with the Text property of the ButtonText textblock.

<Style x:Key="LoginButtonWithText" TargetType="{x:Type Button}" BasedOn="{StaticResource ImgButtonWithText}">
    <Setter Property="ToolTip" Value="Login"></Setter>
    <Setter Property="IsDefault" Value="True"></Setter>
    //How do I set the Image Source value and TextBlock Text value here?
</Style>

I found this question, but it doesn't appear to be quite exactly what I'm looking for. How do I change the value of child controls from an inherited style, or am I doing it all wrong?

Community
  • 1
  • 1
Travis
  • 1,044
  • 1
  • 17
  • 36
  • No you can't do that. You have to re-declare `ContentTemplate` in another style. – Rohit Vats Mar 18 '14 at 17:16
  • Okay - is there a way to establish (for example) that there should always be a margin of 4,0 on the textblock for all styles based on ImgButtonWithText, or am I stuck defining that on every style that inherits from it? – Travis Mar 18 '14 at 17:18
  • If you doesn't redefine `ControlTemplate` in `LoginButtonWithText` or any other style deriving from `ImgButtonWithText`, it will be inherited. Style inheritance works like normal inheritance in C#. Unless you override base property, it will be same. Simple as that. – Rohit Vats Mar 18 '14 at 17:22
  • But in normal inheritance, I can change the values of inherited fields. Here I can't. I'm trying to define a generic style with constraints on controls that must be obeyed by all child styles, which seems like it should be possible. – Travis Mar 18 '14 at 17:26
  • `ContentTemplate` is a property of `Button`. TextBox `Text` is not so why it would be inherited? – Rohit Vats Mar 18 '14 at 17:28
  • If a button is an object, with a member Textbox, in normal inheritance I would be able to access the properties of the inherited Textbox. It seems that this is not how styles work, however. – Travis Mar 18 '14 at 17:51

1 Answers1

0

It seems that what I want to do is not exactly possible, but I've done the next best thing in defining an image button style to use in all styles that inherit from from ImgButtonWithText:

<Style x:Key="ImgButton" TargetType="Button">
    <Setter Property="Background" Value="Transparent"></Setter>
    <Setter Property="Height" Value="30"></Setter>
</Style>
<Style x:Key="ButtonImage" TargetType="Image">
    <Setter Property="Height" Value="20"></Setter>
    <Setter Property="Width" Value="20"></Setter>
    <Setter Property="Stretch" Value="Uniform"></Setter>
</Style>
<Style x:Key="ButtonText" TargetType="TextBlock">
    <Setter Property="Margin" Value="4,0"></Setter>
    <Setter Property="VerticalAlignment" Value="Center"></Setter>
</Style>
<Style x:Key="LoginButtonWithText" TargetType="{x:Type Button}" BasedOn="{StaticResource ImgButton}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Name="ButtonImage" Style="{StaticResource ButtonImage}" Source="../Images/login.png">
                    </Image>
                    <TextBlock Name="ButtonText" Style="{StaticResource ButtonText}" Text="Let's Go"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>
Travis
  • 1,044
  • 1
  • 17
  • 36