设置按钮的内容为<图像和GT;通过样式 [英] Setting Button's Content to <Image> via Styles

查看:142
本文介绍了设置按钮的内容为<图像和GT;通过样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不能得到这个工作:

 <&用户控件GT;
    < UserControl.Resources>
        <&ResourceDictionary中GT;
            <风格X:键=TestStyle的TargetType ={X:类型按钮}>
                < setter属性=Button.Content>
                    < Setter.Value>
                        <图像源=D:\\ TEMP \\ dictionary16.png/>
                    < /Setter.Value>
                < /二传手>
            < /样式和GT;
        < / ResourceDictionary的>
    < /UserControl.Resources>
    < StackPanel的VerticalAlignment =顶部的Horizo​​ntalAlignment =左>
        <按钮样式={StaticResource的TestStyle}/>
        <按钮样式={StaticResource的TestStyle}/>
    < / StackPanel的>
< /用户控件>

这code抛出以下异常(指着第二个按钮):


  

指定的元素已经是另一个元素的逻辑子。首先断开连接。



解决方案

的样式创建图像的一个实例,你不能在两个地方像这样使用它。共享 X创建一个外部资源字典中的形象$ C> = FALSE ,并在风格,那么一个新的将在每次使用的样式地方创建引用它。


例如

 <&用户控件GT;
    < UserControl.Resources>
        <图像X:键=IMGX:共享=假来源=D:\\ TEMP \\ dictionary16.png/>
        <风格X:键=TestStyle的TargetType ={X:类型按钮}>
            < setter属性=内容VALUE ={StaticResource的IMG}/>
        < /样式和GT;
    < /UserControl.Resources>
    < StackPanel的VerticalAlignment =顶部的Horizo​​ntalAlignment =左>
        <按钮样式={StaticResource的TestStyle}/>
        <按钮样式={StaticResource的TestStyle}/>
    < / StackPanel的>
< /用户控件>

Can't get this to work:

<UserControl>
    <UserControl.Resources>
        <ResourceDictionary>
            <Style x:Key="TestStyle" TargetType="{x:Type Button}">
                <Setter Property="Button.Content">
                    <Setter.Value>
                        <Image Source="D:\Temp\dictionary16.png"/>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </UserControl.Resources>
    <StackPanel VerticalAlignment="Top" HorizontalAlignment="Left">
        <Button Style="{StaticResource TestStyle}"/>
        <Button Style="{StaticResource TestStyle}"/>
    </StackPanel>
</UserControl>

This code throws the following exception (pointing to the second button):

Specified element is already the logical child of another element. Disconnect it first.

解决方案

The style creates one instance of the Image, you cannot use it in two places like this. You can create the image in an external resource dictionary with x:Shared= false and reference it in the style then a new one will be created in every place the style is used.


e.g.

<UserControl>
    <UserControl.Resources>
        <Image x:Key="img" x:Shared="false" Source="D:\Temp\dictionary16.png" />
        <Style x:Key="TestStyle" TargetType="{x:Type Button}">
            <Setter Property="Content" Value="{StaticResource img}" />
        </Style>
    </UserControl.Resources>
    <StackPanel VerticalAlignment="Top" HorizontalAlignment="Left">
        <Button Style="{StaticResource TestStyle}" />
        <Button Style="{StaticResource TestStyle}" />
    </StackPanel>
</UserControl>

这篇关于设置按钮的内容为&lt;图像和GT;通过样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆