使用自定义 WPF 对话框控件时的大小问题
我在 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(除了具有明确高度的描述文本框之外)。
为了使此自定义控件的大小正确,我缺少哪个属性设置?
I am defining a custom control in WPF which serves as the base class for multiple dialog windows which all leverage shared services (positioning, Ok and Cancel buttons). The custom control is defined as follows:
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;
}
And the Generic.xaml which defines the template looks like this:
<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>
Finally, I define my new dialog as a derived class:
<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>
Everything behaves as expected, except that the main window's height is way too large. It doesn't seem to fit just its content, but rather extends almost the entire height of the monitor. I went through the code to make sure VerticalAlignments are set to Top (rather than Stretch) and that the Height properties are set to Auto except for the description text box which has an explicit height.
Which property setting am I missing to get this custom control to size correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 ESDialogControl 样式中缺少
SizeToContent="Height"
设置。您还可以取消Height="Auto"
设置,因为这是窗口的默认设置。You are missing the
SizeToContent="Height"
setting in your ESDialogControl style. You can also take away theHeight="Auto"
setting, as that is the default for a Window.