Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying Grid column and row definition by style in resource

Tags:

wpf

xaml

There is an UserControl with the following Grid:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
</Grid>

Now I have a Window and there are I would to write something like that:

<Window.Resources>
    <Style TargetType="Grid">
        <Setter Property="RowDefinitions">
            <Value>
                <RowDefinition Height="*"/>
                <RowDefinition/>
            </Value>
        </Setter>
    </Style>
</Window.Resources>

The key part, which doesn't compile is when I want to change Height from Auto to *. How to do this in legal way?

In general I have to cases. 1) The first row should stretch and the second should be fixed. 2) Vice versa. Maybe a different panel than Grid could be more relevant?

like image 927
Ryszard Dżegan Avatar asked Nov 18 '25 13:11

Ryszard Dżegan


1 Answers

Grid.RowDefinitions and Grid.ColumnDefinitions are no dependency properties, and can't hence be set by a Style.

You may perhaps create a dependency property FirstRowHeight in your UserControl, and bind the Height of the first RowDefinition to that property. Later you may set the FirstRowHeight property in a Style.

<Grid.RowDefinitions>
    <RowDefinition Height="{Binding FirstRowHeight,
        RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"/>
    <RowDefinition/>
</Grid.RowDefinitions>

The property would look like this:

public static readonly DependencyProperty FirstRowHeightProperty =
    DependencyProperty.Register(
        "FirstRowHeight", typeof(GridLength), typeof(YourUserControl));

public GridLength FirstRowHeight
{
    get { return (GridLength)GetValue(FirstRowHeightProperty); }
    set { SetValue(FirstRowHeightProperty, value); }
}

EDIT: In order to support the simple scenario you describe at the end of your question, you may also just have an IsFirstRowFixed dependency property with a property changed callback that sets the row heights in code:

<Grid.RowDefinitions>
    <RowDefinition x:Name="row1" Height="*"/>
    <RowDefinition x:Name="row2" Height="Auto"/>
</Grid.RowDefinitions>

The property:

public static readonly DependencyProperty IsFirstRowFixedProperty =
    DependencyProperty.Register(
        "IsFirstRowFixed", typeof(bool), typeof(UserControl2),
        new PropertyMetadata((o, e) => ((UserControl2)o).IsFirstRowFixedChanged()));

public bool IsFirstRowFixed
{
    get { return (bool)GetValue(IsFirstRowFixedProperty); }
    set { SetValue(IsFirstRowFixedProperty, value); }
}

private void IsFirstRowFixedChanged()
{
    if (IsFirstRowFixed)
    {
        row1.Height = GridLength.Auto;
        row2.Height = new GridLength(1, GridUnitType.Star);
    }
    else
    {
        row1.Height = new GridLength(1, GridUnitType.Star);
        row2.Height = GridLength.Auto;
    }
}
like image 195
Clemens Avatar answered Nov 21 '25 08:11

Clemens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!