Reputation: 1702
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?
Upvotes: 43
Views: 39160
Reputation: 30512
How about IsDefault?
<Button Command="{Binding MyButtonCommand}"
Content="My Button Text"
IsDefault="True"/>
Upvotes: 1
Reputation: 21873
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>
Upvotes: 2
Reputation: 53603
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.
Upvotes: 77