WPF:计算网格列的大小
WPF: Calculating the size of a Grid Column
我有一个包含三列的网格。
<Grid Background="AliceBlue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="8*" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="2*" Name="ManualControlsSplit" />
</Grid.ColumnDefinitions>
第一列包含一个网格和一个图表。
第二列包含一个 GridSplitter。
第三列包含一个 StackPanel,其中包含许多 TextBlock、Button 和包含 TextBlock 和 Button 的网格。文本大小是动态的,基于翻译资源。
我需要计算第三列的内容理想情况下能够绘制的最小宽度,这样内容就不会被剪裁。
我对 WPF 的了解有限google,如有任何帮助,我们将不胜感激。
使用 WPF 中的布局,您可以允许 Grid.Columns
中的一个(如果不是更多的话)成为 <ColumnDefinition Width="Auto"/>
。这实际上告诉 WPF 允许包含的控件需要多少 space。 Auto
的使用也是级联的,所以在你提到的 StackPanel
中,你也可以使它控制宽度 Auto
,就像它包含的项目一样;如果堆栈面板仅包含 TextBlock
(通过某些模板或其他方式),则您还可以将此宽度设置为 Auto
,并根据包含的文本设置宽度。
<Grid Background="AliceBlue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
...
<StackPanel Grid.Column=2
Width="Auto">
<TextBlock Width="Auto"
Text="This is TextBlock"/>
...
</StackPanel>
...
</Grid>
在这种情况下,TextBlock
的 Text
设置 StackPanel
的宽度,进而设置第 3 个网格列的宽度。
希望对您有所帮助。
我的 WPF XAML 设置如下(为了简单起见,删除了一些位):
<UserControl ....>
<Grid Background="AliceBlue" Name="TopGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="12*" Name="Graph" />
<ColumnDefinition Width="5" Name="GridSplitter" />
<ColumnDefinition Width="2*" Name="ManualControlsSplit" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Background="AliceBlue"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<!-- A Graph and simple controls -->
</Grid>
<!-- GRID SPLITTER -->
<GridSplitter Grid.Column="1" Width="5"
HorizontalAlignment="Stretch" Name="Splitter" />
<Label Grid.Column="1" Content="⁞" Foreground="White"
Background="DarkGray"
VerticalAlignment="Center" FontSize="26" FontWeight="Bold"
IsHitTestVisible="False"/>
<!-- end GRID SPLITTER -->
<StackPanel Grid.Column="2" Grid.Row="0" Margin="5"
Name="TemperatureControls">
<!-- Load of Controls -->
</StackPanel>
</Grid>
</UserControl>
要计算所需的宽度,我使用以下代码:
// get my UserControl object
var manualControlView = userControl as HeatingController.Views.ManualControlView;
// Query the current width of THIRD column
double actualWidth = manualControlView.ManualControlsSplit.ActualWidth;
// Set up a BIG size (this has to be bigger size than the contents of the
// THIRD column would ever need)
Size size = new Size(400, manualControlView.TopGrid.ActualHeight);
// Ask WPF layout to calculate the Desired size.
manualControlView.TemperatureControls.Measure(size);
double width = manualControlView.TemperatureControls.DesiredSize.Width;
if (actualWidth <= width)
{
// Too small - do something
}
else
{
// big enough - do something else.
}
变量 'width' 现在包含我要计算的值。
我有一个包含三列的网格。
<Grid Background="AliceBlue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="8*" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="2*" Name="ManualControlsSplit" />
</Grid.ColumnDefinitions>
第一列包含一个网格和一个图表。
第二列包含一个 GridSplitter。
第三列包含一个 StackPanel,其中包含许多 TextBlock、Button 和包含 TextBlock 和 Button 的网格。文本大小是动态的,基于翻译资源。
我需要计算第三列的内容理想情况下能够绘制的最小宽度,这样内容就不会被剪裁。
我对 WPF 的了解有限google,如有任何帮助,我们将不胜感激。
使用 WPF 中的布局,您可以允许 Grid.Columns
中的一个(如果不是更多的话)成为 <ColumnDefinition Width="Auto"/>
。这实际上告诉 WPF 允许包含的控件需要多少 space。 Auto
的使用也是级联的,所以在你提到的 StackPanel
中,你也可以使它控制宽度 Auto
,就像它包含的项目一样;如果堆栈面板仅包含 TextBlock
(通过某些模板或其他方式),则您还可以将此宽度设置为 Auto
,并根据包含的文本设置宽度。
<Grid Background="AliceBlue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
...
<StackPanel Grid.Column=2
Width="Auto">
<TextBlock Width="Auto"
Text="This is TextBlock"/>
...
</StackPanel>
...
</Grid>
在这种情况下,TextBlock
的 Text
设置 StackPanel
的宽度,进而设置第 3 个网格列的宽度。
希望对您有所帮助。
我的 WPF XAML 设置如下(为了简单起见,删除了一些位):
<UserControl ....>
<Grid Background="AliceBlue" Name="TopGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="12*" Name="Graph" />
<ColumnDefinition Width="5" Name="GridSplitter" />
<ColumnDefinition Width="2*" Name="ManualControlsSplit" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Background="AliceBlue"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<!-- A Graph and simple controls -->
</Grid>
<!-- GRID SPLITTER -->
<GridSplitter Grid.Column="1" Width="5"
HorizontalAlignment="Stretch" Name="Splitter" />
<Label Grid.Column="1" Content="⁞" Foreground="White"
Background="DarkGray"
VerticalAlignment="Center" FontSize="26" FontWeight="Bold"
IsHitTestVisible="False"/>
<!-- end GRID SPLITTER -->
<StackPanel Grid.Column="2" Grid.Row="0" Margin="5"
Name="TemperatureControls">
<!-- Load of Controls -->
</StackPanel>
</Grid>
</UserControl>
要计算所需的宽度,我使用以下代码:
// get my UserControl object
var manualControlView = userControl as HeatingController.Views.ManualControlView;
// Query the current width of THIRD column
double actualWidth = manualControlView.ManualControlsSplit.ActualWidth;
// Set up a BIG size (this has to be bigger size than the contents of the
// THIRD column would ever need)
Size size = new Size(400, manualControlView.TopGrid.ActualHeight);
// Ask WPF layout to calculate the Desired size.
manualControlView.TemperatureControls.Measure(size);
double width = manualControlView.TemperatureControls.DesiredSize.Width;
if (actualWidth <= width)
{
// Too small - do something
}
else
{
// big enough - do something else.
}
变量 'width' 现在包含我要计算的值。