Je crée une boîte de dialogue WPF. C'est comme notre bouton normal messagebox
avec ok
et cancel
. Comment créer une telle boîte de dialogue de sorte que le bouton Ok
soit sélectionné lorsque la boîte de dialogue est ouverte ?
Réponses
Trop de publicités?
Kishore Kumar
Points
8636
il suffit de créer un nouveau modèle de bouton et de modifier l'apparence de l'état IsDefault=Tue. Je viens de créer un style et de modifier l'état.
<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>
HaraldDutch
Points
225