C#WPF调整大小控件在网格行填满所有宽度

我试图让我的组合框拉伸使用所有的空余空间中展开的窗口。如果窗口的宽度缩小,ToolBarTray将会崩溃。C#WPF调整大小控件在网格行填满所有宽度

我使用的网格指定为分别* & 300 2列与宽度。 如何制作组合框来填充行中的所有可用空间?

<Grid> 

<Grid.RowDefinitions>

<RowDefinition Height="50" />

</Grid.RowDefinitions>

<DockPanel LastChildFill="True" Grid.Row="0">

<Grid DockPanel.Dock="Top">

<Grid.ColumnDefinitions>

<ColumnDefinition Width="*" />

<ColumnDefinition Width="300" />

</Grid.ColumnDefinitions>

<ToolBarTray DockPanel.Dock="Left">

<ToolBar Name="Standard">

<Button Height="32">

<StackPanel Orientation="Horizontal" Margin="5,0,5,0" HorizontalAlignment="Right">

<Image Height="24" Width="24" HorizontalAlignment="Center" Source="Images/document.png" />

</StackPanel>

</Button>

<Button Height="32">

<StackPanel Orientation="Horizontal" Margin="5,0,5,0" HorizontalAlignment="Right">

<Image Height="24" Width="24" HorizontalAlignment="Center" Source="Images/document.png" />

</StackPanel>

</Button>

<Button Height="32">

<StackPanel Orientation="Horizontal" Margin="5,0,5,0" HorizontalAlignment="Right">

<Image Height="24" Width="24" HorizontalAlignment="Center" Source="Images/document.png" />

</StackPanel>

</Button>

</ToolBar>

<ToolBar Name="Standard2">

<Button Height="32">

<StackPanel Orientation="Horizontal" Margin="5,0,5,0" HorizontalAlignment="Right">

<Image Height="24" Width="24" HorizontalAlignment="Center" Source="Images/document.png" />

</StackPanel>

</Button>

<Button Height="32">

<StackPanel Orientation="Horizontal" Margin="5,0,5,0" HorizontalAlignment="Right">

<Image Height="24" Width="24" HorizontalAlignment="Center" Source="Images/document.png" />

</StackPanel>

</Button>

<Button Height="32">

<StackPanel Orientation="Horizontal" Margin="5,0,5,0" HorizontalAlignment="Right">

<Image Height="24" Width="24" HorizontalAlignment="Center" Source="Images/document.png" />

</StackPanel>

</Button>

</ToolBar>

</ToolBarTray>

<ComboBox Grid.Column="1" MinWidth="200" ></ComboBox>

</Grid>

</DockPanel>

</Grid>

回答:

只要改变你的列宽的定义:

<Grid DockPanel.Dock="Top"> 

<Grid.ColumnDefinitions>

<ColumnDefinition Width="Auto" />

<ColumnDefinition Width="*" />

</Grid.ColumnDefinitions>

默认情况下,组合框已设置为Stretch水平和垂直对齐。

可能是第一列,你可以把一个硬编码的大小。

但是,如果你的GUI需要有更多的空间来还要好,把周围的ToolbarTray一些利润率和ComboBox

问候

回答:

使用的Horizo​​ntalAlignment = “STRECH”

请参见下面的代码

<ComboBox Grid.Column="1" MinWidth="200" HorizontalAlignment="Stretch"></ComboBox> 

回答:

好吧,如果你希望工具栏完全崩溃了,我找到的唯一解决方案是编写代码一点点。 仅在XAML中进行布局会更加优雅,但有时​​C#代码是唯一的方法。

  1. 摆脱电网:

  2. 处理画布的大小调整:

    private void ToolbarSizeChanged(object sender, SizeChangedEventArgs e) 

    {

    const double COMBO_MIN_DESIRED_WIDTH = 300.0;

    Size newSize = e.NewSize;

    Size sizeToolbarTray = new Size(Double.PositiveInfinity, newSize.Height);

    // measure to update DesiredSize

    toolBarTray.Measure(sizeToolbarTray);

    Rect toolBarTrayRect = new Rect(0, 0, 0, newSize.Height);

    Rect comboRect =new Rect(0,0,0,newSize.Height);

    // 3 cases :

    // 1) Much space is available

    if (newSize.Width > toolBarTray.DesiredSize.Width + COMBO_MIN_DESIRED_WIDTH)

    {

    toolBarTrayRect.Width = toolBarTray.DesiredSize.Width;

    comboRect.X = toolBarTrayRect.Width;

    comboRect.Width = newSize.Width -toolBarTrayRect.Width;

    }

    // 2) Space to show Combo, but not totally toolbar

    else if (newSize.Width > COMBO_MIN_DESIRED_WIDTH)

    {

    toolBarTrayRect.Width = newSize.Width - COMBO_MIN_DESIRED_WIDTH;

    comboRect.X = toolBarTrayRect.Width;

    comboRect.Width = COMBO_MIN_DESIRED_WIDTH;

    }

    // 3) Not enough space to show toolbar

    else

    {

    toolBarTrayRect.Width = 0;

    comboRect.Width = newSize.Width;

    }

    // Layout the two components :

    toolBarTray.Arrange(toolBarTrayRect);

    combobox.Arrange(comboRect);

    }

这里到workiing解决方案的链接,让你CANN检查它的你想要什么:
http://1drv.ms/1MySnde

希望我理解你希望它可以帮助,视

以上是 C#WPF调整大小控件在网格行填满所有宽度 的全部内容, 来源链接: utcz.com/qa/262837.html

回到顶部