Silverlight: Why does this style not work?

I am using Silverlight 4. I have a button:

        <Button Click="addTopicButton_Click">
            <Image Source="/PlumPudding;component/Images/appbar.add.rest.png" />
        </Button>

It looks good. However, when I try to install it Contentusing Style, the content does not appear:

    <Style x:Name="AddButton" TargetType="Button">
        <Setter Property="Content">
            <Setter.Value>
                <Image Source="/PlumPudding;component/Images/appbar.add.rest.png" />
            </Setter.Value>
        </Setter>
    </Style>

    <Button Click="addTopicButton_Click" Style="{StaticResource AddButton}" />

The button is empty. Why is this?

+3
source share
3 answers

It is not recommended to include UIElements, such as Imagein style. Such an object is created only once when the style is merged during Xaml parsing. It is important to understand UIElements that one instance can appear only once in the visual tree. So even if your code worked, it would work only on one button, any other button trying to use the same style would fail.

ContentTemplate : -

<Style x:Key="AddButton" TargetType="Button"> 
    <Setter Property="ContentTemplate"> 
        <Setter.Value> 
            <DataTemplate>
                <Image Source="/PlumPudding;component/Images/appbar.add.rest.png" /> 
            </DataTemplate>
        </Setter.Value> 
    </Setter> 
</Style> 

<Button Click="addTopicButton_Click" Style="{StaticResource AddButton}" /> 

DataTemplate, , . Image.

+5

x: Key, Style, x: Name

+1

Your code requires two changes.

  • Changed x:Nameto x:Key, and refer to it if you want to use it using StaticResource.

  • Change it

    <Setter.Value> 
            <Image Source="whatever..." /> 
    </Setter.Value> 
    

    <Setter.Value> 
        <DataTemplate>
            <Image Source="whatever..." /> 
        </DataTemplate>
    </Setter.Value> 
    

See if this helps you!

0
source

Source: https://habr.com/ru/post/1779771/


All Articles