wpf 如何数据绑定 ColumnDefinition 的宽度或 RowDefinition 的高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/147908/
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
How do I databind a ColumnDefinition's Width or RowDefinition's Height?
提问by Nidonocu
Under the View-Model-ViewModel pattern for WPF, I am trying to databind the Heights and Widths of various definitions for grid controls, so I can store the values the user sets them to after using a GridSplitter. However, the normal pattern doesn't seem to work for these particular properties.
在 WPF 的 View-Model-ViewModel 模式下,我试图对网格控件的各种定义的高度和宽度进行数据绑定,以便在使用 GridSplitter 后存储用户设置的值。但是,正常模式似乎不适用于这些特定属性。
Note: I'm posting this as a reference question that I'm posting as Google failed me and I had to work this out myself. My own answer to follow.
注意:我将此作为参考问题发布,因为谷歌让我失败了,我必须自己解决这个问题。我自己的答案要遵循。
采纳答案by Nidonocu
There were a number of gotchas I discovered:
我发现了许多陷阱:
- Although it may appear like a double in XAML, the actual value for a *Definition's Height or Width is a 'GridLength' struct.
- All the properties of GridLength are readonly, you have to create a new one each time you change it.
- Unlike every other property in WPF, Width and Height don't default their databinding mode to 'TwoWay', you have to manually set this.
- 尽管它在 XAML 中可能看起来像一个双精度值,但 *Definition 的 Height 或 Width 的实际值是一个 'GridLength' 结构。
- GridLength 的所有属性都是只读的,每次更改时都必须创建一个新属性。
- 与 WPF 中的所有其他属性不同,Width 和 Height 的数据绑定模式不会默认为“TwoWay”,您必须手动设置它。
Thusly, I used the following code:
因此,我使用了以下代码:
private GridLength myHorizontalInputRegionSize = new GridLength(0, GridUnitType.Auto)
public GridLength HorizontalInputRegionSize
{
get
{
// If not yet set, get the starting value from the DataModel
if (myHorizontalInputRegionSize.IsAuto)
myHorizontalInputRegionSize = new GridLength(ConnectionTabDefaultUIOptions.HorizontalInputRegionSize, GridUnitType.Pixel);
return myHorizontalInputRegionSize;
}
set
{
myHorizontalInputRegionSize = value;
if (ConnectionTabDefaultUIOptions.HorizontalInputRegionSize != myHorizontalInputRegionSize.Value)
{
// Set the value in the DataModel
ConnectionTabDefaultUIOptions.HorizontalInputRegionSize = value.Value;
}
OnPropertyChanged("HorizontalInputRegionSize");
}
}
And the XAML:
和 XAML:
<Grid.RowDefinitions>
<RowDefinition Height="*" MinHeight="100" />
<RowDefinition Height="Auto" />
<RowDefinition Height="{Binding Path=HorizontalInputRegionSize,Mode=TwoWay}" MinHeight="50" />
</Grid.RowDefinitions>
回答by Greg Sansom
Create a IValueConverter
as follows:
创建一个IValueConverter
如下:
public class GridLengthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double val = (double)value;
GridLength gridLength = new GridLength(val);
return gridLength;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
GridLength val = (GridLength)value;
return val.Value;
}
}
You can then utilize the converter in your Binding:
然后,您可以在绑定中使用转换器:
<UserControl.Resources>
<local:GridLengthConverter x:Key="gridLengthConverter" />
</UserControl.Resources>
...
<ColumnDefinition Width="{Binding Path=LeftPanelWidth,
Mode=TwoWay,
Converter={StaticResource gridLengthConverter}}" />
回答by JustinMichel
The easiest solution is to simply use string settings for these properties so that WPF will automatically support them using GridLengthConverter without any extra work.
最简单的解决方案是简单地为这些属性使用字符串设置,以便 WPF 将使用 GridLengthConverter 自动支持它们,而无需任何额外工作。
回答by Joel B Fant
Another possibility, since you brought up converting between GridLength
and int
, is to create an IValueConverter
and use it when binding to Width
. IValueConverter
s also handle two-way binding because they have both ConvertTo()
and ConvertBack()
methods.
另一种可能性,因为你提出了在GridLength
和之间的转换int
,是创建一个IValueConverter
并在绑定到 时使用它Width
。IValueConverter
s 也处理双向绑定,因为它们同时具有ConvertTo()
andConvertBack()
方法。