我在WPF中定义了一个自定义控件,它作为多个对话窗口的基类,所有对话窗口都利用共享服务(定位,确定和取消按钮)。自定义控件定义如下:
public class ESDialogControl : Window
{
static ESDialogControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ESDialogControl), new FrameworkPropertyMetadata(typeof(ESDialogControl)));
}
internal ESDialogControl() { }
public ESDialogControl(string title)
{
Title = title;
this.KeyDown += new KeyEventHandler(ESDialogControl_KeyDown);
}
void ESDialogControl_KeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Escape: cancelButton_Click(null, null); break;
case Key.Enter: okButton_Click(null, null); break;
default: break;
}
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_okButton = base.GetTemplateChild("OkButton") as Button;
_okButton.IsEnabled = false;
_okButton.Click += new RoutedEventHandler(okButton_Click);
Button cancelButton = base.GetTemplateChild("CancelButton") as Button;
cancelButton.Click += new RoutedEventHandler(cancelButton_Click);
}
protected Button OkButton { get { return _okButton; } }
void cancelButton_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
void okButton_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
Close();
}
Button _okButton;
}
定义模板的Generic.xaml如下所示:
<Style TargetType="{x:Type local:ESDialogControl}">
<Setter Property="Width" Value="600" />
<Setter Property="Height" Value="Auto" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ESDialogControl}">
<Grid Height="Auto" Background="Beige" VerticalAlignment="Top" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="1" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ContentPresenter Grid.Row="0"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Content="{TemplateBinding Content}" Margin="{TemplateBinding Padding}" />
<Rectangle Grid.Row="1" Fill="Navy" />
<StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right">
<Button x:Name="OkButton" Width="70" Margin="0 10 10 0">OK</Button>
<Button x:Name="CancelButton" Width="70" Padding="2" Margin="0 10 10 0">Cancel</Button>
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
最后,我将新对话框定义为派生类:
<local:ESDialogControl x:Class="Mercersoft.Economica.Studio.View.DialogWindows.NewModelDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Mercersoft.Economica.Studio.View">
<Grid Background="Yellow" VerticalAlignment="Top" Height="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right">Name:</Label>
<TextBox x:Name="ItemName" x:FieldModifier="public" Grid.Column="1"
HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch"
VerticalContentAlignment="Center" TextAlignment="Left"
TextChanged="ItemName_TextChanged" />
<Label Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right">Description:</Label>
<TextBox x:Name="ItemDescription" x:FieldModifier="public" Grid.Row="1" Grid.Column="1"
AcceptsReturn="True" TextWrapping="Wrap" Height="140"
HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch"
VerticalContentAlignment="Center" TextAlignment="Left" />
</Grid>
</local:ESDialogControl>
除了主窗口的高度太大外,一切都按预期运行。它似乎不仅仅适合其内容,而是几乎延伸到显示器的整个高度。我仔细检查了代码以确保VerticalAlignments设置为Top(而不是Stretch),并且Height属性设置为Auto,除了具有明确高度的描述文本框。
我缺少哪个属性设置才能使此自定义控件的大小正确?
答案 0 :(得分:2)
您缺少ESDialogControl样式中的SizeToContent="Height"
设置。您还可以取消Height="Auto"
设置,因为这是窗口的默认设置。