aku
aku

Reputation: 123974

WPF: resize parent container when child's ControlTemplate changes

I have a custom error template for my TextBox. Something like this:

<Style TargetType="{x:Type TextBox}" x:Key="ErrorTemplateStyle">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <StackPanel Orientation="Vertical">
                    <Border BorderBrush="Orange" BorderThickness="1">
                        <AdornedElementPlaceholder />
                    </Border>
                    <TextBlock Text="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem}" />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

My TextBox is placed inside of DataGrid's cell. Now when error occurs I want size of that cell to be changed to accomodate new template for my TextBox. Any ideas how this behavior can be achieved?

Upvotes: 2

Views: 2046

Answers (1)

John Fisher
John Fisher

Reputation: 22719

Option 1: Create a property in a new or existing class which holds the desired width of the cell. (Make sure the class and property implement INotifyPropertyChanged.) Bind the width of the cell to that property. Change the property value when the error takes place, and the cell should adjust it's size.

Option 2: Use something other than a grid (DockPanel, StackPanel, etc.). Properly arranging these things will allow the container to automatically size to fit the content.

Option 3: Figure out a way to use the grid's sizing properties. Setting Width="Auto" normally allows the column to size to it's content, but that can depend upon the other columns in the grid.

Upvotes: 2

Related Questions