样式仅在Grid.Resources外部时才适用于第一次出现 [英] Style only works for the first occurence when outside Grid.Resources?

查看:157
本文介绍了样式仅在Grid.Resources外部时才适用于第一次出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有数据模板的gridview,其中包括一个切换按钮. Gridview中项目的数据模板中有一个网格,带有一个样式化的切换按钮.切换按钮的样式位于Grid.Resources中.它工作正常,但是当我将样式从Grid.Resources移到Page.Resources或App.xaml时,该样式中定义的按钮内的内容会消失,除了在网格视图中第一次出现该按钮之外.这是样式:

I have a gridview with a data template which includes a togglebutton. The datatemplate for the items in the Gridview has a grid in it, with a stylized togglebutton. The style for the togglebutton is located in Grid.Resources. It works fine, but when I move the style from Grid.Resources to either Page.Resources or App.xaml, the content inside the button that is defined in the style disappears from all except the first occurrence of the button in the gridview. Here is the style:

<Style TargetType="ToggleButton" x:Key="teststyle">
        <Setter Property="Padding" Value="0"/>
        <Setter Property="Content">
            <Setter.Value>
                <Path HorizontalAlignment="Center" Stroke="Black" StrokeThickness="1.25"  VerticalAlignment="Center" Height="9" Width="9"  Stretch="Uniform" Fill="Black" Data="M 0,0 -11.78,-11.779 0,-23.561 l 1.061,1.061 -9.97,9.971 21.064,0 0,1.5 -21.064,0 9.97,9.968 L 0,0 Z" RenderTransformOrigin="0.5,0.5" >
                    <Path.RenderTransform>
                        <CompositeTransform Rotation="-90"/>
                    </Path.RenderTransform>
                </Path>
            </Setter.Value>
        </Setter>
</Style>

以下是该行为的屏幕截图: http://imgur.com/a/8iZaD 图像是样式位于Grid.Resources中时的原始图像,底部是样式移动时的图像.

Here are screenshots of the behavior: http://imgur.com/a/8iZaD The top image is the original one when the style was located in Grid.Resources, and the bottom is when the style gets moved.

推荐答案

您可以通过使用样式中的ContentTemplate代替Content来解决此问题.

You can workaround this behavior by using ContentTemplate in your style instead Content.

<Page.Resources>
    <Style TargetType="ToggleButton">
        <Setter Property="Padding" Value="0"/>
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Path HorizontalAlignment="Center" Stroke="Black" StrokeThickness="1.25"  VerticalAlignment="Center" Height="9" Width="9"  Stretch="Uniform" Fill="Black" Data="M 0,0 -11.78,-11.779 0,-23.561 l 1.061,1.061 -9.97,9.971 21.064,0 0,1.5 -21.064,0 9.97,9.968 L 0,0 Z" RenderTransformOrigin="0.5,0.5" >
                        <Path.RenderTransform>
                            <CompositeTransform Rotation="-90"/>
                        </Path.RenderTransform>
                    </Path>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>

这篇关于样式仅在Grid.Resources外部时才适用于第一次出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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