如何数据绑定 ColumnDefinition 的宽度或 RowDefinition 的高度? [英] How do I databind a ColumnDefinition's Width or RowDefinition's Height?
本文介绍了如何数据绑定 ColumnDefinition 的宽度或 RowDefinition 的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 WPF 的 View-Model-ViewModel 模式下,我试图对网格控件的各种定义的高度和宽度进行数据绑定,以便我可以存储用户在使用 GridSplitter 后设置的值.但是,正常模式似乎不适用于这些特定属性.
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.
注意:我将此作为参考问题发布,因为谷歌让我失败了,我必须自己解决这个问题.要遵循我自己的答案.
推荐答案
我发现了许多问题:
- 尽管它在 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");
}
}
还有 XAML:
<Grid.RowDefinitions>
<RowDefinition Height="*" MinHeight="100" />
<RowDefinition Height="Auto" />
<RowDefinition Height="{Binding Path=HorizontalInputRegionSize,Mode=TwoWay}" MinHeight="50" />
</Grid.RowDefinitions>
这篇关于如何数据绑定 ColumnDefinition 的宽度或 RowDefinition 的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文