【问题标题】:ContentDialog: Applying both Style and PrimaryButtonStyleContentDialog:同时应用 Style 和 PrimaryButtonStyle
【发布时间】:2018-06-14 15:50:21
【问题描述】:

帮派,

我指的是 16299 版本。

我正在尝试为 ContentDialog 设置样式,并为 PrimaryButton 设置样式。但是,我不能让他们两个一起工作:

<ContentDialog x:Class="App1.ContentDialog1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  PrimaryButtonText="Button1"
  SecondaryButtonText="Button2" 
  Style="{StaticResource ContentDialogStyle}" 
  PrimaryButtonStyle="{StaticResource ContentDialogPrimaryButtonStyle}">
    <Grid />
</ContentDialog>

这只会应用“样式”而不是“主按钮样式”。但是,如果我删除“样式”属性,我就会应用 PrimaryButtonStyle。

我尝试在 Style XAML 中应用 PimaryButtonStyle,但这也不起作用:

<Style TargetType="Button" x:Key="ContentDialogPrimaryButtonStyle">
  <Setter Property="Template">
    <Setter.Value>
      ...
    </Setter.Value>
  </Setter>
</Style>

<Style TargetType="ContentDialog" x:Key="ContentDialogStyle">
  <Setter Property="PrimaryButtonStyle" Value="{StaticResource ContentDialogPrimaryButtonStyle}" />
  <Setter Property="Template">
    <Setter.Value>
      ...
    </Setter.Value>
  </Setter>
</Style>

有什么想法可以在 ContentDialog 中设置主要/次要按钮的样式吗?

亲切的问候

亚当

【问题讨论】:

  • 您是否使用自己的按钮覆盖模板?
  • 是的,正在尝试。
  • 如果您要覆盖模板,您可以使用您自己的按钮,使用您想要的任何样式,就像使用任何普通按钮一样。 :)
  • 到目前为止,我一直在这样做,但我自己的模板(在 xbox 上)面临着一大堆焦点问题。看起来 ContentDialog 有 Primary 和 Secondary 按钮,并且有一个属性来设置它们的样式,我想尽可能地使用原生的方法。如果我不设置 ContentDialog 的样式,设置样式可以正常工作,但如果我这样做会被忽略:(

标签: xaml uwp


【解决方案1】:

它应该与ContentDialogStyle中的Template相关。我们可以同时更改xaml中的ContentDialogStylePrimaryButtonStyle,这里有一个简单的例子,你可以看看。然后你应该在ContentDialogStyle 中查看你的Template

这是代码,

<Style TargetType="Button" x:Key="ContentDialogPrimaryButtonStyle">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Border BorderBrush="Green" BorderThickness="5">
                    <ContentPresenter Background="Red" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style TargetType="ContentDialog" x:Key="ContentDialogStyle">
    <Setter Property="Background" Value="Yellow"/>
</Style>

这是内容对话框,

<ContentDialog
    ...
    PrimaryButtonText="Button1"
    SecondaryButtonText="Button2"
    Style="{StaticResource ContentDialogStyle}"
    PrimaryButtonStyle="{StaticResource ContentDialogPrimaryButtonStyle}">

    <Grid/>

</ContentDialog>

【讨论】:

  • 但在这种情况下,我们不会覆盖 ContentDialog 上的模板,我需要这样做。我需要将模板设置为 ContentDialog 和 PrimaryButton 的自定义模板。
  • 我可以直接在 ContentDialog 模板中设置 PrimaryButton 样式,但这违背了在 ContentDialog 上设置 PrimaryButtonStyle 的目的,因为它并不难融入 ContentDialog 的样式。
  • 我上面的代码只是一个示例,以阐明我们可以更改这两种样式,对于您的这个问题,您应该查看 ContentDialog 的自定义模板。简而言之,例如,如果您的 ContentDialog 自定义模板没有 PrimaryButton 或 PrimaryButton 完全不同,那么 PrimaryButtonStyle 属性不应该适用于它。
  • 两种样式都是我直接从 MSDN 中提取的,所以模板是相同的(除了颜色变化来测试是否应用了样式)。
  • 你已经将应用定位在16299上,你可以发现Windows.UI.Xaml.Controls.ContentDialog的默认样式已经被这个document改变了,你可以在里面找到对应的ContentDialog样式generic.xaml 文件,该文件应位于路径 C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.16299.0\Generic 中。使用此模板,您应该可以使用 PrimaryButtonStyle 属性。
猜你喜欢
  • 2020-04-09
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多