首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Stackpanel in style

Stackpanel in style
EN

Stack Overflow用户
提问于 2011-09-14 18:41:59
回答 2查看 2.5K关注 0票数 1

使用windows.resource中的下一块代码。

代码语言:javascript
复制
<Style TargetType="Button" x:Key="l1" >
    <Setter Property="Button.Effect" >
    <!--<Setter Property="BitmapEffect">-->
        <Setter.Value>
            <DropShadowEffect />
        </Setter.Value>
    </Setter>
    <Setter Property="Content" >
        <Setter.Value >
            <StackPanel  Orientation="Horizontal">
                <Image Source="Resources\find.bmp" Stretch="Uniform" ></Image>
                <TextBlock>Find</TextBlock>
            </StackPanel>
        </Setter.Value>
    </Setter>
</Style>

它只适用于一个按钮,但只要我将它应用于在运行时产生的第二个按钮错误。

代码语言:javascript
复制
<Button Height="23" HorizontalAlignment="Left" Margin="322,25,0,0" Name="Button18" VerticalAlignment="Top" Width="75" Style="{StaticResource l1}" />
<Button Height="23" HorizontalAlignment="Left" Margin="586,37,0,0" Name="Button19" VerticalAlignment="Top" Width="75" Style="{StaticResource l1}" />

有什么解决方案可以解决这个问题吗?

EN

回答 2

Stack Overflow用户

发布于 2011-09-14 18:53:35

您不能以这种方式直接设置ContentControlContent。这样做的原因是,Content设置器中的StackPanel (仅命名一个)对于应用该样式的所有按钮都是相同的实例;但是,这是不允许的(可能会导致您得到的元素已经是另一个元素异常的子元素)。

相反,应设置ContentTemplate属性:

代码语言:javascript
复制
<Setter Property="ContentTemplate">
    <Setter.Value>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Source="Resources\find.bmp" Stretch="Uniform" ></Image>
                <TextBlock>Find</TextBlock>
            </StackPanel>
        </DataTemplate>
    </Setter.Value>
</Setter>

这将会起作用,因为现在将为模板的每个实例化创建一个新的可视化树分支(即,将有与您的按钮一样多的StackPanel等)。

票数 0
EN

Stack Overflow用户

发布于 2011-09-14 18:54:12

您不能通过这样的样式设置控件的内容(至少不能设置两次)。

您应该使用模板来发送内容,如下所示:

代码语言:javascript
复制
    <DataTemplate x:Key="buttonTemplate">
        <StackPanel  Orientation="Horizontal">
            <Image Source="Resources\find.bmp" Stretch="Uniform" ></Image>
            <TextBlock>Find</TextBlock>
        </StackPanel>
    </DataTemplate>
    <Style TargetType="Button" x:Key="l1" >
        <Setter Property="Button.Effect" >
            <Setter.Value>
                <DropShadowEffect />
            </Setter.Value>
        </Setter>
        <Setter Property="ContentTemplate" Value="{StaticResource buttonTemplate}" />
    </Style>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7414990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档