【问题标题】:Setting content in an inherited (BasedOn) style以继承的 (BasedOn) 样式设置内容
【发布时间】:2023-04-03 05:03:01
【问题描述】:

我有一个基本风格,比如说:

<Style x:Key="DefaultButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="{StaticResource DefaultButtonBackground}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}"> 
                <Grid x:Name="ButtonShapeGrid" Height="{TemplateBinding Height}" Width="{TemplateBinding Height}">
                    <!-- snip some shaping Xaml. -->

                    <ContentPresenter 
                        Grid.RowSpan="2"
                        VerticalAlignment="{TemplateBinding VerticalAlignment}" HorizontalAlignment="Center" />

                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

现在,我需要创建一些样式,用固定的东西替换按钮的内容......就像这样(其中 CloseButtonGlyph 是路径资源)。这工作正常:

<Style x:Key="CloseButtonStyle" BasedOn="{StaticResource DefaultButtonStyle}" TargetType="{x:Type Button}">
    <Setter Property="Content" Value="{StaticResource CloseButtonGlyph}" />
</Style>

但是,我还需要在内容中添加任意 Xaml,这就是我遇到问题的地方。在 WPF 窗口或控件中使用样式时,以下内容不起作用(我收到一个设计时错误,指出内容无法从 ContentElement 或 Visual 派生):

<Style x:Key="WingDingStyle" BasedOn="{StaticResource DefaultButtonStyle}" TargetType="{x:Type Button}">
    <Setter Property="Content">
        <Setter.Value>
             <TextBlock FontFamily="WebDings" Text="3" FontSize="18" Foreground="White" />
        </Setter.Value>
    </Setter>
</Style>

我不知道我需要做什么。将 TextBox 放在窗口或控件的按钮内容中效果很好,为什么不放在样式中?

【问题讨论】:

    标签: wpf styles


    【解决方案1】:

    对不起!上次来不及测试一次,这段代码运行良好

    <Style TargetType="{x:Type Button}">
        <Style.Resources>
            <TextBlock x:Key="myContent" FontFamily="WebDings" Background="Red" Text="3" FontSize="18" Foreground="White" />
        </Style.Resources>
        <Setter Property="Content" Value="{DynamicResource myContent}"/>
    </Style>
    

    像这样更改设置器也可以正常工作

    <Setter Property="Content" Value="{Binding Source={StaticResource myContent}}"/>
    

    希望对你有帮助!!

    【讨论】:

    • 这行得通,谢谢。不知道为什么简单地设置内容不起作用。
    【解决方案2】:

    试试这样的:

    <Style x:Key="WingDingStyle" BasedOn="{StaticResource DefaultButtonStyle}" TargetType="{x:Type Button}">
        <Style.Resources>
            <TextBlock x:Key="myContent" FontFamily="WebDings" Text="3" FontSize="18" Foreground="White" />
        </Style.Resources>
        <Setter Property="Content" Value={StaticResource myContent}/>
    </Style>
    

    希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      我认为问题在于样式在多个按钮之间共享,因此 WPF 试图为每个按钮添加相同的 TextBlock(显然 TextBlock 只能有一个 UI 元素作为其父元素)。

      尝试将样式默认设置为“不共享”,因此通过添加x:Shared 属性为每个按钮创建一个新的TextBlock:

      <Style x:Key="WingDingStyle" x:Shared="False" ...>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-26
        • 2014-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多