如何在WPF中设置Grid ColumnDefinitions的样式 [英] how to style Grid ColumnDefinitions in WPF

查看:2203
本文介绍了如何在WPF中设置Grid ColumnDefinitions的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何设计一个Grid,以便我不需要指定

 < Grid .ColumnDefinitions> 
< ColumnDefinition Width =autoSharedSizeGroup =SG1/>
< ColumnDefinition Width =autoSharedSizeGroup =SG2/>
< /Grid.ColumnDefinitions>

每次都是?

非常感谢很多!



ps:我确实尝试在Google上搜索。但我找不到任何答案。任何人都可以从谷歌找到答案,请告诉我你用什么关键字搜索?有时我很难确定搜索关键字的用途。



ps2:我太懒惰了,我只是打开chrome并键入内容并搜索。如果没有发现,我没有发现任何结果并来到这里。有没有人会在Google上搜索,然后找不到,然后打开bing.com并搜索?而且什么也找不到,去雅虎搜索和搜索?.....

必须写出 RowDefinitions ColumnDefinitions ,所以有一天我厌倦了它,写了一些附加属性可用于此类事物。

现在不用像我这样编写我的 Grid 定义:

 < Grid> 
< Grid.RowDefinitions>
< RowDefinition Height =Auto/>
< RowDefinition Height =Auto/>
< RowDefinition Height =Auto/>
< RowDefinition Height =Auto/>
< RowDefinition Height =Auto/>
< RowDefinition Height =*/>
< /Grid.RowDefinitions>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =Auto/>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =Auto/>
< ColumnDefinition Width =*/>
< /Grid.ColumnDefinitions>
< / Grid>

我可以使用

 < Grid local:GridHelpers.RowCount =6
local:GridHelpers.StarRows =5
local:GridHelpers.ColumnCount =4
local: GridHelpers.StarColumns = 1,3 >

< / Grid>

只允许自动 * 大小,但大部分时间都是我使用的。



它还支持动态大小的Grids的绑定

 < Grid local:GridHelpers.RowCount ={Binding RowCount}
local:GridHelpers.ColumnCount ={ Binding ColumnCount}/>






下面是代码的一个副本,网站不断下降:

  public class GridHelpers 
{
#region RowCount属性

///< summary>
///将指定数量的行添加到RowDefinitions。
///默认高度是自动
///< / summary>
public static readonly DependencyProperty RowCountProperty =
DependencyProperty.RegisterAttached(
RowCount,typeof(int),typeof(GridHelpers),
new PropertyMetadata(-1,RowCountChanged));

//获取
public static int GetRowCount(DependencyObject obj)
{
return(int)obj.GetValue(RowCountProperty);


// Set
public static void SetRowCount(DependencyObject obj,int value)
{
obj.SetValue(RowCountProperty,value);
}

//更改事件 - 添加行
public static void RowCountChanged(
DependencyObject obj,DependencyPropertyChangedEventArgs e)
{
if (!(obj是Grid)||(int)e.NewValue< 0)
return;

Grid grid =(Grid)obj;
grid.RowDefinitions.Clear();

for(int i = 0; i<(int)e.NewValue; i ++)
grid.RowDefinitions.Add(
new RowDefinition(){Height = GridLength。自动});

SetStarRows(grid);
}

#endregion

#region ColumnCount属性

///< summary>
///将指定数量的列添加到ColumnDefinitions。
///默认宽度是自动
///< / summary>
public static readonly DependencyProperty ColumnCountProperty =
DependencyProperty.RegisterAttached(
ColumnCount,typeof(int),typeof(GridHelpers),
new PropertyMetadata(-1,ColumnCountChanged));

//获取
public static int GetColumnCount(DependencyObject obj)
{
return(int)obj.GetValue(ColumnCountProperty);


// Set
public static void SetColumnCount(DependencyObject obj,int value)
{
obj.SetValue(ColumnCountProperty,value);
}

//更改事件 - 添加列
public static void ColumnCountChanged(
DependencyObject obj,DependencyPropertyChangedEventArgs e)
{
if (!(obj是Grid)||(int)e.NewValue< 0)
return;

Grid grid =(Grid)obj;
grid.ColumnDefinitions.Clear();

for(int i = 0; i<(int)e.NewValue; i ++)
grid.ColumnDefinitions.Add(
new ColumnDefinition(){Width = GridLength。自动});

SetStarColumns(grid);
}

#endregion

#region StarRows属性

///< summary>
///使指定的行高等于Star。
///可以设置多行
///< / summary>
public static readonly DependencyProperty StarRowsProperty =
DependencyProperty.RegisterAttached(
StarRows,typeof(string),typeof(GridHelpers),
new PropertyMetadata(string.Empty,StarRowsChanged)) ;
$ b $ //获取
public static string GetStarRows(DependencyObject obj)
{
return(string)obj.GetValue(StarRowsProperty);


Set b $ b public static void SetStarRows(DependencyObject obj,string value)
{
obj.SetValue(StarRowsProperty,value);
}

//更改事件 - 使指定的行高等于Star
public static void StarRowsChanged(
DependencyObject obj,DependencyPropertyChangedEventArgs e)
{
if(!(obj is Grid)|| string.IsNullOrEmpty(e.NewValue.ToString()))
return;

SetStarRows((Grid)obj);
}

#endregion

#region StarColumns属性

///< summary>
///使指定的列宽等于星。
///可以在多列上设置
///< / summary>
public static readonly DependencyProperty StarColumnsProperty =
DependencyProperty.RegisterAttached(
StarColumns,typeof(string),typeof(GridHelpers),
new PropertyMetadata(string.Empty,StarColumnsChanged)) ;
$ b $ //获取
public static string GetStarColumns(DependencyObject obj)
{
return(string)obj.GetValue(StarColumnsProperty);


// Set
public static void SetStarColumns(DependencyObject obj,string value)
{
obj.SetValue(StarColumnsProperty,value);
}

//更改事件 - 使指定的列宽等于星
public static void StarColumnsChanged(
DependencyObject obj,DependencyPropertyChangedEventArgs e)
{
if(!(obj is Grid)|| string.IsNullOrEmpty(e.NewValue.ToString()))
return;

SetStarColumns((Grid)obj);
}

#endregion

private static void SetStarColumns(Grid grid)
{
string [] starColumns =
GetStarColumns (网格).Split( '');

for(int i = 0; i< grid.ColumnDefinitions.Count; i ++)
{
if(starColumns.Contains(i.ToString()))
grid.ColumnDefinitions [i] .Width =
new GridLength(1,GridUnitType.Star);



private static void SetStarRows(Grid grid)
{
string [] starRows =
GetStarRows(grid).Split ( '');

for(int i = 0; i< grid.RowDefinitions.Count; i ++)
{
if(starRows.Contains(i.ToString()))
grid.RowDefinitions [i] .Height =
new GridLength(1,GridUnitType.Star);
}
}
}


I want to know how can I style a Grid so that I don't need to specify the

<Grid.ColumnDefinitions>
   <ColumnDefinition Width="auto" SharedSizeGroup="SG1"/>
   <ColumnDefinition Width="auto" SharedSizeGroup="SG2"/>
</Grid.ColumnDefinitions>

every time?

Thank you very much!

ps: I did try to search on Google first. But I couldn't find any answer. Anyone who find the answer from google could you please tell me what keyword do you use to search? Sometimes I find it hard to determine what keyword use to search.

ps2: I am too lazy, every I just open chrome and type something and search. If nothing found, I conclude nothing found and come here. Is there anyone will search on Google, then find nothing, then open bing.com and search? And find nothing and go to yahoo and search and search?.....

解决方案

It was always a pet peeve of mine to have to write out the RowDefinitions and ColumnDefinitions, so one day I got tired of it and wrote some attached properties that can be used for this kind of thing.

Now instead of writing my Grid definition like this:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
</Grid>

I can use

<Grid local:GridHelpers.RowCount="6"
      local:GridHelpers.StarRows="5"
      local:GridHelpers.ColumnCount="4"
      local:GridHelpers.StarColumns="1,3">

</Grid>

It only allows for Auto and * sizes, but most of the time that's all I'm using.

It also supports bindings for dynamically sized Grids

<Grid local:GridHelpers.RowCount="{Binding RowCount}"
      local:GridHelpers.ColumnCount="{Binding ColumnCount}" />


Here's a copy of the code in case that site ever goes down :

public class GridHelpers
{
    #region RowCount Property

    /// <summary>
    /// Adds the specified number of Rows to RowDefinitions. 
    /// Default Height is Auto
    /// </summary>
    public static readonly DependencyProperty RowCountProperty =
        DependencyProperty.RegisterAttached(
            "RowCount", typeof(int), typeof(GridHelpers),
            new PropertyMetadata(-1, RowCountChanged));

    // Get
    public static int GetRowCount(DependencyObject obj)
    {
        return (int)obj.GetValue(RowCountProperty);
    }

    // Set
    public static void SetRowCount(DependencyObject obj, int value)
    {
        obj.SetValue(RowCountProperty, value);
    }

    // Change Event - Adds the Rows
    public static void RowCountChanged(
        DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        if (!(obj is Grid) || (int)e.NewValue < 0)
            return;

        Grid grid = (Grid)obj;
        grid.RowDefinitions.Clear();

        for (int i = 0; i < (int)e.NewValue; i++)
            grid.RowDefinitions.Add(
                new RowDefinition() { Height = GridLength.Auto });

        SetStarRows(grid);
    }

    #endregion

    #region ColumnCount Property

    /// <summary>
    /// Adds the specified number of Columns to ColumnDefinitions. 
    /// Default Width is Auto
    /// </summary>
    public static readonly DependencyProperty ColumnCountProperty =
        DependencyProperty.RegisterAttached(
            "ColumnCount", typeof(int), typeof(GridHelpers),
            new PropertyMetadata(-1, ColumnCountChanged));

    // Get
    public static int GetColumnCount(DependencyObject obj)
    {
        return (int)obj.GetValue(ColumnCountProperty);
    }

    // Set
    public static void SetColumnCount(DependencyObject obj, int value)
    {
        obj.SetValue(ColumnCountProperty, value);
    }

    // Change Event - Add the Columns
    public static void ColumnCountChanged(
        DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        if (!(obj is Grid) || (int)e.NewValue < 0)
            return;

        Grid grid = (Grid)obj;
        grid.ColumnDefinitions.Clear();

        for (int i = 0; i < (int)e.NewValue; i++)
            grid.ColumnDefinitions.Add(
                new ColumnDefinition() { Width = GridLength.Auto });

        SetStarColumns(grid);
    }

    #endregion

    #region StarRows Property

    /// <summary>
    /// Makes the specified Row's Height equal to Star. 
    /// Can set on multiple Rows
    /// </summary>
    public static readonly DependencyProperty StarRowsProperty =
        DependencyProperty.RegisterAttached(
            "StarRows", typeof(string), typeof(GridHelpers),
            new PropertyMetadata(string.Empty, StarRowsChanged));

    // Get
    public static string GetStarRows(DependencyObject obj)
    {
        return (string)obj.GetValue(StarRowsProperty);
    }

    // Set
    public static void SetStarRows(DependencyObject obj, string value)
    {
        obj.SetValue(StarRowsProperty, value);
    }

    // Change Event - Makes specified Row's Height equal to Star
    public static void StarRowsChanged(
        DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        if (!(obj is Grid) || string.IsNullOrEmpty(e.NewValue.ToString()))
            return;

        SetStarRows((Grid)obj);
    }

    #endregion

    #region StarColumns Property

    /// <summary>
    /// Makes the specified Column's Width equal to Star. 
    /// Can set on multiple Columns
    /// </summary>
    public static readonly DependencyProperty StarColumnsProperty =
        DependencyProperty.RegisterAttached(
            "StarColumns", typeof(string), typeof(GridHelpers),
            new PropertyMetadata(string.Empty, StarColumnsChanged));

    // Get
    public static string GetStarColumns(DependencyObject obj)
    {
        return (string)obj.GetValue(StarColumnsProperty);
    }

    // Set
    public static void SetStarColumns(DependencyObject obj, string value)
    {
        obj.SetValue(StarColumnsProperty, value);
    }

    // Change Event - Makes specified Column's Width equal to Star
    public static void StarColumnsChanged(
        DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        if (!(obj is Grid) || string.IsNullOrEmpty(e.NewValue.ToString()))
            return;

        SetStarColumns((Grid)obj);
    }

    #endregion

    private static void SetStarColumns(Grid grid)
    {
        string[] starColumns = 
            GetStarColumns(grid).Split(',');

        for (int i = 0; i < grid.ColumnDefinitions.Count; i++)
        {
            if (starColumns.Contains(i.ToString()))
                grid.ColumnDefinitions[i].Width = 
                    new GridLength(1, GridUnitType.Star);
        }
    }

    private static void SetStarRows(Grid grid)
    {
        string[] starRows = 
            GetStarRows(grid).Split(',');

        for (int i = 0; i < grid.RowDefinitions.Count; i++)
        {
            if (starRows.Contains(i.ToString()))
                grid.RowDefinitions[i].Height = 
                    new GridLength(1, GridUnitType.Star);
        }
    }
}

这篇关于如何在WPF中设置Grid ColumnDefinitions的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆