如何在wpf对话框中选择默认按钮?

发布于 2024-12-07 06:55:32 字数 142 浏览 1 评论 0原文

我正在创建一个 WPF 对话框。它就像我们普通的带有 okcancel 按钮的 messagebox 。如何创建这样一个对话框,以便在打开对话框时选择Ok按钮?

I am creating a WPF dialog. It is like our normal messagebox with ok and cancel button. How to create such a dialog so that the Ok button is selected when the dialog is opened?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆° 2024-12-14 06:55:32

设置窗口的默认按钮

设置默认按钮的 IsDefault 属性 设置为 true。

请注意,您还可以通过设置按钮的 IsCancel 属性 设置为 true。


在窗口中设置选定(聚焦)按钮

如果您想选择特定按钮,请使用 Focus 方法,如下所示:

yourButton.Focus();

您可以在窗口加载时执行此操作(在 Window_Loaded 事件中)。

要在窗口打开时选择特定按钮,请确保其 IsTabStop 属性 设置为 true 并确保其 TabIndex 属性 低于窗口上的任何其他控件。

To set a Window's Default button

Set your default button's IsDefault property to true.

Note that you can also set a Window's Cancel button by setting a button's IsCancel property to true.


To set the Selected (focused) button in a Window

If you want to select a particular button then use the Focus method like this:

yourButton.Focus();

You might do this when a Window loads (in the Window_Loaded event).

To select a particular button when your Window opens make sure its IsTabStop property is set to true and ensure its TabIndex property is lower than any other control on the Window.

寒江雪… 2024-12-14 06:55:32

只需创建一个新的按钮模板并更改 IsDefault=Tue 状态的外观。
我刚刚创建了一种样式并修改了状态。

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
x:Class="WpfApplication7.Window3"
x:Name="Window"
Title="Window3"
Width="640" Height="480" FocusManager.FocusedElement="{Binding ElementName=test}">

<Window.Resources>
    <Style x:Key="ButtonFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Margin="2"
                    Stroke="red" StrokeThickness="1"
                     SnapsToDevicePixels="true"  StrokeDashArray="1 2"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="border" BorderThickness="1" BorderBrush="#FF040000" CornerRadius="5">
                        <Border.Background>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="#FF7A7A7A" Offset="0"/>
                                <GradientStop Color="#FFE7E7E7" Offset="1"/>
                            </LinearGradientBrush>
                        </Border.Background>
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                                                    <Trigger Property="IsDefault" Value="True">
                            <Setter Property="Background" TargetName="border">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#FFA76F6F" Offset="0"/>
                                        <GradientStop Color="#FFE7E7E7" Offset="1"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsFocused" Value="True"/>
                                <Condition Property="IsDefault" Value="True"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" TargetName="border">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#FFC2BE5B" Offset="0.007"/>
                                        <GradientStop Color="#FFE7E7E7" Offset="1"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                        </MultiTrigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="BorderBrush" TargetName="border" Value="#FF01641D"/>
                            <Setter Property="BorderThickness" TargetName="border" Value="2"/>
                            <Setter Property="Background" TargetName="border">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#FF528159" Offset="0"/>
                                        <GradientStop Color="#FFE7E7E7" Offset="1"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<Grid x:Name="LayoutRoot">
    <Button x:Name="test" Content="Button" HorizontalAlignment="Left" Height="26" Margin="130,157,0,0" VerticalAlignment="Top" Width="164" Style="{DynamicResource ButtonStyle1}" IsDefault="True"/>
    <Button Content="Button" Height="26" Margin="298,157,162,0" VerticalAlignment="Top" Style="{DynamicResource ButtonStyle1}"/>
    <Button Content="Button" HorizontalAlignment="Right" Height="26" Margin="0,157,-6,0" VerticalAlignment="Top" Width="164" Style="{DynamicResource ButtonStyle1}"/>
</Grid>

just create a new button template and change the look and feel for the IsDefault=Tue state.
I just created a style and modified the state.

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
x:Class="WpfApplication7.Window3"
x:Name="Window"
Title="Window3"
Width="640" Height="480" FocusManager.FocusedElement="{Binding ElementName=test}">

<Window.Resources>
    <Style x:Key="ButtonFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Margin="2"
                    Stroke="red" StrokeThickness="1"
                     SnapsToDevicePixels="true"  StrokeDashArray="1 2"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="border" BorderThickness="1" BorderBrush="#FF040000" CornerRadius="5">
                        <Border.Background>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="#FF7A7A7A" Offset="0"/>
                                <GradientStop Color="#FFE7E7E7" Offset="1"/>
                            </LinearGradientBrush>
                        </Border.Background>
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                                                    <Trigger Property="IsDefault" Value="True">
                            <Setter Property="Background" TargetName="border">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#FFA76F6F" Offset="0"/>
                                        <GradientStop Color="#FFE7E7E7" Offset="1"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsFocused" Value="True"/>
                                <Condition Property="IsDefault" Value="True"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" TargetName="border">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#FFC2BE5B" Offset="0.007"/>
                                        <GradientStop Color="#FFE7E7E7" Offset="1"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                        </MultiTrigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="BorderBrush" TargetName="border" Value="#FF01641D"/>
                            <Setter Property="BorderThickness" TargetName="border" Value="2"/>
                            <Setter Property="Background" TargetName="border">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#FF528159" Offset="0"/>
                                        <GradientStop Color="#FFE7E7E7" Offset="1"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<Grid x:Name="LayoutRoot">
    <Button x:Name="test" Content="Button" HorizontalAlignment="Left" Height="26" Margin="130,157,0,0" VerticalAlignment="Top" Width="164" Style="{DynamicResource ButtonStyle1}" IsDefault="True"/>
    <Button Content="Button" Height="26" Margin="298,157,162,0" VerticalAlignment="Top" Style="{DynamicResource ButtonStyle1}"/>
    <Button Content="Button" HorizontalAlignment="Right" Height="26" Margin="0,157,-6,0" VerticalAlignment="Top" Width="164" Style="{DynamicResource ButtonStyle1}"/>
</Grid>

无言温柔 2024-12-14 06:55:32

IsDefault 怎么样?

<Button Command="{Binding MyButtonCommand}"
        Content="My Button Text"
        IsDefault="True"/>

How about IsDefault?

<Button Command="{Binding MyButtonCommand}"
        Content="My Button Text"
        IsDefault="True"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文