C# 如何以这种方式调整WPF网格面板的大小?
我有一个非常简单的窗口,网格包含两列,一个TextBlock和一个TextBox 我需要它将列0设置为自动调整其内容大小,并将列1(内容)设置为列0大小的4倍 我该怎么做呢。如果这是解决方案,我将创建一个网格子体,因为我确实需要这个功能 编辑:更多说明。列0的内容在运行时不会更改,因此列0或列1的大小在运行时不得更改。网格将是配置了C# 如何以这种方式调整WPF网格面板的大小?,c#,.net,wpf,xaml,grid-layout,C#,.net,Wpf,Xaml,Grid Layout,我有一个非常简单的窗口,网格包含两列,一个TextBlock和一个TextBox 我需要它将列0设置为自动调整其内容大小,并将列1(内容)设置为列0大小的4倍 我该怎么做呢。如果这是解决方案,我将创建一个网格子体,因为我确实需要这个功能 编辑:更多说明。列0的内容在运行时不会更改,因此列0或列1的大小在运行时不得更改。网格将是配置了SizeToContent=“widthandeheight”的窗口的子窗口,因此不必存在额外的空间 回答Dmitry:我用以下代码尝试了您所说的内容,但根本不起作
SizeToContent=“widthandeheight”
的窗口的子窗口,因此不必存在额外的空间
回答Dmitry:我用以下代码尝试了您所说的内容,但根本不起作用:
<Window x:Class="UnderstandSizing.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
SizeToContent="WidthAndHeight" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".25*" />
<ColumnDefinition Width=".75*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Text="THIS IS THE LABEL:" />
<TextBox Grid.Column="1" Text="content" />
</Grid>
</Window>
最后一次编辑:为什么我(或任何人)需要这个
WPF的优点之一是它能够在没有固定大小的情况下工作,对吗?如果你是受损的,你有一个更大的字体大小,一切看起来都很好。如果你把你的UI翻译成另一种需要x2大小的语言,一切看起来都会很好。如果你有不同的PPI设置,一切都会很好
但我不想看到屏幕在运行时改变大小,因为用户不习惯这样。这就是为什么我要将输入字段的大小设置为已知字段的倍数。如果我让标签单元格根据需要重新调整大小,然后我将内容单元格设置为标签的倍数,我将获得自动调整大小的好处,用户希望具有固定大小屏幕的行为(除非他们更改其大小)。取自:
其想法是使用:
更新2-发布完整的XAML(请注意网格子节点的行为):
我刚检查过它,它成功了
更新:简单地说,在WPF中没有现成的方法来实现这一点。根据你的环境,你必须想出一些技巧来让你的网格工作
另一个问题是-通常WPF布局设计为保护性的,即如果网格的子级不能缩小-通常布局逻辑应该适应它,通常通过挂起一些布局规则 编辑:
如果在编译时知道大小,手动设置宽度不是更容易吗
您可以使用BindingConverter,我将使用水平StackPanel中的单独项目(看看StackPanel的宽度是否足够容纳您的内容): 这是清理后的代码 MainWindow.xaml:
<!-- define xmlns:local="clr-namespace:YourAppNamespace" -->
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<StackPanel.Resources>
<local:WidthConverter x:Key="wc" />
</StackPanel.Resources>
<Grid Background="Gray" x:Name="col1">
<TextBlock Text="blub"/>
</Grid>
<Grid Background="Orange" Width="{Binding ElementName=col1, Path=ActualWidth, Converter={StaticResource ResourceKey=wc}}">
<Label Content="bla"></Label>
</Grid>
</StackPanel>
可以在网格列上使用绑定:
<Grid.ColumnDefinitions>
<ColmunDefinition Width="Auto" x:Name="firstCol"/>
<ColumnDefinition Width="{Binding ActualWidth, ElementName=firstCol, Converter={StaticResource MultiplyConverter}, ConverterParameter=4}" />
</Grid.ColumnDefinitions>
您还可以为网格列编写附加属性。据我所知,您正在尝试布局标签(textblock)及其相应的输入字段。作为一个指标,您应该首先看到关于简单数据输入表单的说明,该表单是关于开发
数据输入的
表单,但布局也得到了很好的演示
特别是观察她如何使用designer、properties窗口和XAML
在WPF窗口上布局控件,然后我认为您不需要将第一列设置为Auto
大小,第二列设置为4*
第一列
编辑:根据相关更新,我发布了一个可能的解决方案,如下所示:
窗口中的SizeToContent
用法,并且绑定到Textbox
控件的ActualWidth
属性):
.Xaml.cs
文件代码(注意这里的转换器):
使用System.Windows;
使用System.Windows.Data;
命名空间网格测试
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
}
公共类FourWidthConverter:IValueConverter
{
公共对象转换(对象值、System.Type targetType、对象参数、System.Globalization.CultureInfo区域性)
{
返回4*(双精度)值;
}
公共对象转换回(对象值、System.Type targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新系统。NotImplementedException();
}
}
}
当第1列内容的大小增加时,是否希望网格的外部大小扩大?或者你是否有一个给定的空间,网格必须留在其中?或者第1列的宽度是常量,并且在编译时已知?一个问题:如果希望第1列的大小是第0列的四倍,那么剩余的空间会怎么样?或者,如果列0的宽度+4倍于列0的宽度超出可用空间,您想要什么?您是否尝试使用百分比-@Dmitry
0.2*
和0.8*
与*
和4*
相同,后者更接近SoMoS所寻找的。@All:添加了更多解释,以帮助您理解问题。谢谢大家。恐怕这根本不能回答问题。当列0内容的大小更改时,如何更改网格的宽度?问题是:您尝试过吗?您的网格将始终扩展到整个宽度,列0通常没有其内容的确切宽度。不,不是,Martin。将两个按钮放入网格并尝试。这是因为按钮会根据网格大小自动调整大小。将第一个按钮的宽度设置为“20”,然后查看第二列的宽度是否为“60”
public class WidthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is double))
{
return Binding.DoNothing;
}
return ((double)value) * 4.0;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
<Grid.ColumnDefinitions>
<ColmunDefinition Width="Auto" x:Name="firstCol"/>
<ColumnDefinition Width="{Binding ActualWidth, ElementName=firstCol, Converter={StaticResource MultiplyConverter}, ConverterParameter=4}" />
</Grid.ColumnDefinitions>
public class MultiplyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double originalValue;
double multiplier;
if (value != null && parameter != null &&
double.TryParse(value.ToString(), out originalValue) &&
double.TryParse(parameter.ToString(), out multiplier)) //Can be lots of things: sentinel object, NaN (not a number)...
{
return originalValue * multiplier;
}
else return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
using System.Windows;
using System.Windows.Data;
namespace GridTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
public class FourWidthConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return 4 * (double)value;
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new System.NotImplementedException();
}
}
}