Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 875613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:17:56+00:00 2026-05-15T11:17:56+00:00

I am defining a custom control in WPF which serves as the base class

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T11:17:57+00:00Added an answer on May 15, 2026 at 11:17 am

    You are missing the SizeToContent="Height" setting in your ESDialogControl style. You can also take away the Height="Auto" setting, as that is the default for a Window.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.