|
John, I looked into it some more and apparently we've both been over-engineering this. To set the initial focus you don't need a behavior. You simply set TabIndex="0" as in the following example.
The missing link in Cocktail is to set TabIndex="0" on the ContentControl in the dialog host. I will change this for the next release, but you don't have to wait for it. Cocktail actually allows you to replace the out-of-the-box dialog host with your own.
To do this, add a ChildWindow to your solution and replace the XAML with the following. This is the exact same XAML taken from Cocktail except for TabIndex="0" on the ContentControl. Make sure you replace the x:Class with whatever you want to call it.
<controls:ChildWindow x:Class="MyApp.CustomDialogHostView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="277"
d:DesignWidth="800"
HasCloseButton="False"
mc:Ignorable="d">
<Grid x:Name="LayoutRoot"
MinWidth="166"
Margin="2">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ItemsControl x:Name="DialogButtons" Grid.Row="1">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Width="75"
Height="23"
Margin="5"
cal:Message.Attach="Close($dataContext)"
Content="{Binding Content}"
IsEnabled="{Binding Enabled}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ContentControl x:Name="ActiveItem"
Width="Auto"
Height="Auto"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
TabIndex="0" />
</Grid>
</controls:ChildWindow>Now, all you have to do is create a corresponding ViewModel that extends from DialogHostBase. Cocktail will discover your custom ViewModel and through the normal Caliburn.Micro convention it will find your custom view.
namespace MyApp
{
public class CustomDialogHostViewModel : DialogHostBase
{
}
}
|