在 WPF 中使用 * 以编程方式设置网格列的宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9803710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:20:13  来源:igfitidea点击:

Programmatically setting the width of a grid column with * in WPF

wpfgrid

提问by user589195

I want to programmatically configure a wpf grid.

我想以编程方式配置 wpf 网格。

I want to be able to set a grid with 2 columns, the first taking up 20% of available space, the second taking up 80%. In xaml I would use the * operator but I cant work out how to do this programmatically.

我希望能够设置一个包含 2 列的网格,第一个占用 20% 的可用空间,第二个占用 80%。在 xaml 中,我会使用 * 运算符,但我无法弄清楚如何以编程方式执行此操作。

In Xaml I would do:

在 Xaml 中,我会这样做:

<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition width="20*" />
    <ColumnDefinition width="80*" />
</Grid>

In code I want to do:

在代码中我想做:

Grid grid = new Grid();
grid.ColumnDefinitions.Add( new ColumnDefinition(20*) );
grid.ColumnDefinitions.Add( new ColumnDefinition(80*) );

Please could someone advise.

请有人建议。

回答by Klaus78

Grid grid = new Grid();
ColumnDefinition c1 = new ColumnDefinition();
c1.Width = new GridLength(20, GridUnitType.Star);
ColumnDefinition c2 = new ColumnDefinition();
c2.Width = new GridLength(80, GridUnitType.Star);
grid.ColumnDefinitions.Add(c1);
grid.ColumnDefinitions.Add(c2);

回答by Venugopal M

Suppose you have some buttons (aligned horizontally) in a page and need to hide / show certain ones depending on some status.

假设您在页面中有一些按钮(水平对齐)并且需要根据某些状态隐藏/显示某些按钮。

<Grid HorizontalAlignment="Center" Grid.Column="1" Width="340" VerticalAlignment="Center" Background="Transparent">
        <Grid.ColumnDefinitions>
                <ColumnDefinition Width="2*" x:Name="colOne"></ColumnDefinition>
                <ColumnDefinition Width="0" x:Name="colTwo"></ColumnDefinition>
                <ColumnDefinition Width="0" x:Name="colThree"></ColumnDefinition>
                <ColumnDefinition Width="0" x:Name="colFour"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <Button x:Name="btnOne"  Grid.Column="0" Height="50" Width="50" Content="One" Cursor="Hand" />
        <Button x:Name="btnTwo"  Grid.Column="1" Height="50" Width="50" Content="Two" Cursor="Hand" />
        <Button x:Name="btnThree"  Grid.Column="2" Height="50" Width="50" Content="Thre" Cursor="Hand" />
        <Button x:Name="btnFour"  Grid.Column="3" Height="50" Width="50" Content="Four" Cursor="Hand" />
</Grid>

Here btnOne will be visible in the page when executed. btnOne will also be aligned in the center. Now if we want Three and Four to be displayed and One to be hidden when clicked on One, we can use this code:

这里 btnOne 将在执行时在页面中可见。btnOne 也将在中心对齐。现在,如果我们希望在单击“一”时显示“三”和“四”并隐藏“一”,我们可以使用以下代码:

private void btnOne_Click(object sender, RoutedEventArgs e)
{
    SetGridColWidth(colOne, false);
    SetGridColWidth(colThree, true);
    SetGridColWidth(colFour, true);
}

private void SetGridColWidth(ColumnDefinition column, bool show)
{
    if (show)
        column.Width = new GridLength(2, GridUnitType.Star);
    else
        column.Width = new GridLength(0);
}

You can toggle between visibility of any button at runtime.

您可以在运行时在任何按钮的可见性之间切换。

Hope this helps someone !

希望这有助于某人!

回答by Sathya Ram

In MVVM way:
------------
*XAML(View) code:*

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="{Binding FirstColumn}"/>
            <ColumnDefinition Width="{Binding SecondColumn}"/>
        </Grid.ColumnDefinitions>
    </Grid>

**ViewModel (C#) code**

public class MyViewModel : BindableBase
{
    private GridLength _firstColumn;
    private GridLength _secondColumn;

    public MyViewModel()
    {
        _firstColumn = new GridLength(75, GridUnitType.Star);
        _secondColumn = new GridLength(25, GridUnitType.Star);
    }

    public GridLength FirstColumn
    {
        get { return _firstColumn; }
        set { SetProperty(ref _firstColumn, value); }
    }

    public GridLength SecondColumn
    {
        get { return _secondColumn; }
        set { SetProperty(ref _secondColumn, value); }
    }

    private void NotifyToggleFullScreen(bool isToggleExpansion)
    {
        if (isToggleExpansion)
        {
            FirstColumn = new GridLength(0, GridUnitType.Auto);
            SecondColumn = new GridLength(100, GridUnitType.Star);
        }
        else
        {
            FirstColumn = new GridLength(75, GridUnitType.Star);
            SecondColumn = new GridLength(25, GridUnitType.Star);
        }
    }
}