MessageBoxGeneric.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:JetSun.Presentation" xmlns:js="clr-namespace:JetSun.Presentation.Markups"> <Style TargetType="local:MessageBoxControl"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="local:MessageBoxControl"> <Grid FlowDirection="{Binding ContentFlowDirection}" MaxWidth="600"> <Grid.RowDefinitions> <RowDefinition Height="55" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Image Source="{Binding MessageImageSource}" HorizontalAlignment="Left" VerticalAlignment="Center" Height="32" Width="32" Margin="10 0 0 0" /> <TextBlock Text="{Binding Message}" Grid.RowSpan="2" Grid.Column="1" TextWrapping="Wrap" TextAlignment="Left" HorizontalAlignment="{Binding ContentTextAlignment}" VerticalAlignment="Center" Margin="10 10 10 10" /> <Border Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" BorderThickness="0 1 0 0" Margin="3"> <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0 0 5 0" > <Button Content="是" Visibility="{Binding YesNoVisibility}" Command="{Binding YesCommand}" IsDefault="{Binding IsYesDefault}" Margin="5 5 5 5" Width="80" x:Name="PART_YesButton" IsTabStop="True" TabIndex="0"/> <Button Content="否" Visibility="{Binding YesNoVisibility}" Command="{Binding NoCommand}" IsDefault="{Binding IsNoDefault}" Margin="5 5 5 5" Width="80" x:Name="PART_NoButton" IsTabStop="True" TabIndex="1" /> <Button Content="确认" Visibility="{Binding OkVisibility}" Command="{Binding OkCommand}" IsDefault="{Binding IsOkDefault}" Margin="5 5 5 5" Width="80" x:Name="PART_OkButton" IsTabStop="True" TabIndex="2" /> <Button Content="取消" Visibility="{Binding CancelVisibility}" Command="{Binding CancelCommand}" IsDefault="{Binding IsCancelDefault}" Margin="5 5 5 5" Width="80" x:Name="PART_CancelButton" IsTabStop="True" TabIndex="3" /> </StackPanel> </Border> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
取出控件模板的控件实例方法:
public class MessageBoxControl : Control { static MessageBoxControl() { DefaultStyleKeyProperty.OverrideMetadata(typeof(MessageBoxControl), new FrameworkPropertyMetadata(typeof(MessageBoxControl))); } Button _btnYes; Button _btnNo; Button _btnOk; Button _btnCancel; public override void OnApplyTemplate() { base.OnApplyTemplate(); _btnYes = GetTemplateChild("PART_YesButton") as Button; _btnNo = GetTemplateChild("PART_NoButton") as Button; _btnOk = GetTemplateChild("PART_OkButton") as Button; _btnCancel = GetTemplateChild("PART_CancelButton") as Button; } protected override void OnGotFocus(RoutedEventArgs e) { MessageBoxViewModel model = this.DataContext as MessageBoxViewModel; if (model.IsYesDefault) { _btnYes.Focus(); } if (model.IsNoDefault) { _btnNo.Focus(); } if (model.IsOkDefault) { _btnOk.Focus(); } if (model.IsCancelDefault) { _btnCancel.Focus(); } } }