代码之家  ›  专栏  ›  技术社区  ›  Philipp Schmid

使用自定义WPF对话框控件时的大小调整问题

  •  2
  • Philipp Schmid  · 技术社区  · 14 年前

    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(自动),除了具有显式高度的description文本框。

    1 回复  |  直到 14 年前
        1
  •  2
  •   Ray Burns    14 年前

    你错过了 SizeToContent="Height" 在ESDialogControl样式中设置。你也可以拿走 Height="Auto" 设置,因为这是窗口的默认设置。