简体   繁体   English

在Windows Phone通用应用程序中更改内容对话框按钮的样式

[英]Change the style of content dialog button in windows phone universal application

I have this content dialog box defined inside my xaml: 我在我的xaml中定义了这个内容对话框:

    <ContentDialog x:Name="AlertMessage" Background="#363636" IsSecondaryButtonEnabled="True" SecondaryButtonText="Cancel"  IsPrimaryButtonEnabled="True" PrimaryButtonText="Ok" >
        <ContentDialog.Content>
            <StackPanel Name="rootStackPanel" Height="Auto"  >
                <StackPanel Margin="0">
                    <StackPanel Margin="0,0,0,10" Orientation="Horizontal">
                        <TextBlock x:Name="HeadingText" x:FieldModifier="public" Style="{StaticResource ApplicationMessageBoxHeadingStyle}" Text="Alert"  />
                        <Image Margin="10,05,0,0" Source="/Assets/Images/alert.png" Width="35"></Image>
                    </StackPanel>
                    <TextBlock x:FieldModifier="public" x:Name="ContentText" Style="{StaticResource ApplicationMessageBoxErrorStyle}" Text="Are you sure you want to log off ?" />
                </StackPanel>
            </StackPanel>
        </ContentDialog.Content>
    </ContentDialog>

And I'm calling it like this: 而我这样称呼它:

private void AppBarButton_Click(object sender, RoutedEventArgs e)
    {
        MessageBox();
    }
    private async void MessageBox()
    {
        ContentDialogResult LogoutDialog = await AlertMessage.ShowAsync();

        if (LogoutDialog == ContentDialogResult.Primary)
        {
            this.Frame.Navigate(typeof(MainPage));
        }
        else
        {
            // User pressed Cancel or the back arrow.
            // Terms of use were not accepted.
        }

    }

Problem: is that my app have a style for all the buttons in my applications. 问题:我的应用程序是否具有应用程序中所有按钮的样式。 And I want to apply same styles to the primary and secondary buttons of that dialog box too. 我想将相同的样式应用于该对话框的主要和次要按钮。 And I am unable to figure out how to Achieve this. 我无法弄清楚如何实现这一目标。 Is it possible to apply styles to these buttons? 是否可以将样式应用于这些按钮?

If you want to change the ContendDialog PrimaryButtonSettings you need inside ContenDialog.Resources just to set a Style that you are targeting Buttons and the Setter of With Property="" and Value="" and that should fix below is one example. 如果你想在ContenDialog.Resources中更改你需要的ContendDialog PrimaryButtonSettings,只是为了设置你要定位按钮的样式和具有Property =“”和Value =“”的Setter,下面应该修复的是一个例子。

Converter={StaticResource StringEmptyToBoolConverter}}"
<ContentDialog.Resources>
    <converters:StringEmptyToBoolConverter x:Key="StringEmptyToBoolConverter"/>
    <Style TargetType="Button">
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="FontSize" Value="13.6"/>
        <Setter Property="Width" Value="Auto"/>
    </Style>
</ContentDialog.Resources>
<WebView Height="400" Width="500" DefaultBackgroundColor="Transparent" Source="{Binding State.GuideUri}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="RulesWebView"/>

You can indirectly customize the buttons. 您可以间接自定义按钮。 The ContentDialog's buttons use the "default" style for the target type "Button". ContentDialog的按钮使用“默认”样式作为目标类型“按钮”。 You can overwrite the ContentDialog Style of the SDK and add a different Button Style in the Setter of the Template. 您可以覆盖SDK的ContentDialog样式,并在模板的Setter中添加不同的Button Style。 This button style will just be valid in the scope of the ContentDialog control. 此按钮样式仅在ContentDialog控件的范围内有效。

In this example the new button style needs to be put in the first border element (x:Name="Container") 在此示例中,需要将新按钮样式放在第一个边框元素中(x:Name =“Container”)

<Border.Resources>
  <Style TargetType="Button" BasedOn="{StaticResource YourNormalButtonStyle}">
    <Setter Property="BorderBrush" Value="Red"/>
    <Setter Property="Foreground" Value="Red"/>
  </Style>
</Border.Resources>

ContentDialog按钮不可自定义,但您可以不设置ContentDialog的主要和辅助按钮,并在模板中添加自己的按钮。

I used this code to change Button color on my UWP app. 我使用此代码更改了我的UWP应用程序上的Button颜色。

 var cd = new ContentDialog();
 cd.Content = "Remove file ?";
 cd.Title = "Remove file";
 cd.PrimaryButtonText = "OK";
 cd.SecondaryButtonText = "Cancel";
 var bst = new Windows.UI.Xaml.Style(typeof(Button));
 bst.Setters.Add(new Setter(Button.BackgroundProperty, Colors.Red));
 bst.Setters.Add(new Setter(Button.ForegroundProperty, Colors.White));
 cd.PrimaryButtonStyle = bst;
 var ress = await cd.ShowAsync();

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM