Home Setting columnDefinition and RowDefinition from style template?
Reply: 2

Setting columnDefinition and RowDefinition from style template?

Bill Moore
1#
Bill Moore Published in 2018-02-11 16:29:46Z

Is it possible to add the follow code:

<Grid.RowDefinitions>
    <RowDefinition Height="auto"/>
    <RowDefinition Height="*"/>
    <RowDefinition Height="auto"/>
</Grid.RowDefinitions>

into a Style / Setter template for a Grid? And then automatically setup the RowDefinitions for a Grid like this?

<Grid style="{StaticResource MyRowDefs}">
    <Button  Grid.Row="0" Content="Button1/>
    <TextBox Grid.Row="1"/>
    <Button  Grid.Row="2" Content="Button2/>
</Grid>
Christian H.
2#
Christian H. Reply to 2018-02-11 20:37:27Z

Do this solve your issue: How to create reusable WPF grid layout

For your example, you could do something like:

<Window x:Class="ReUseGridTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ReUseGridTest"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="GridItemsStyle" TargetType="ItemsControl">
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                        </Grid>
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <ItemsControl Style="{StaticResource GridItemsStyle}">
        <Button Grid.Row="0" Content="Button1"/>
        <TextBox Grid.Row="1" Text="Test"/>
        <Button  Grid.Row="2" Content="Button2"/>
    </ItemsControl>
</Window>
mm8
3#
mm8 Reply to 2018-02-12 13:25:47Z

You can't set the RowDefinitions property in a Style because it's not a dependency property but you can create a custom Grid type and use this one:

public class CustomGrid : Grid
{
    public CustomGrid()
    {
        RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
        RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) };
        RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
    }
}

Usage:

<local:CustomGrid>
    <Button  Grid.Row="0" Content="Button1/>
    <TextBox Grid.Row="1"/>
    <Button  Grid.Row="2" Content="Button2/>
</local:CustomGrid>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.351698 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO