本文介绍了Wpf GridSplitter 替换 row.height 属性上的绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难使用网格拆分器.我已将 RowDefinition.Height 依赖属性绑定到模型的 clr 属性,如下所示.

<RowDefinition Height='{绑定路径=Height, Mode=OneWay}'/><行定义高度='*'/></Grid.RowDefinitions>

这在使用 GridSplitter 之前工作正常.当使用 GridSplitter 手动更改行高时,它会将绑定替换为新的固定大小(并删除绑定).

您是否有任何想法或解决方法如何创建两行可以使用 GridSplitter 调整大小但仍根据 clr 属性/绑定更改其高度?

解决方案

我认为问题在于您的源属性 Height 是 double 类型,而 RowDefinition.HeightGridLength 类型.使用转换器,它会双向工作

<Grid.RowDefinitions><RowDefinition Height="{绑定路径=高度,模式=双向,转换器={StaticResource DoubleGridLengthConverter}}"/><!--...--></Grid.RowDefinitions>

DoubleGridLengthConverter

公共类 DoubleGridLengthConverter : IValueConverter{public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfoculture){返回新的 GridLength((double)value);}public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfoculture){GridLength gridLength = (GridLength) 值;返回gridLength.Value;}}

更新
在此处上传了我的示例应用程序:http://www.mediafire.com/download.php?pgibb205d65596q

通过在下部 TextBox 中输入一个值来设置 RowDefinition.Height 并使用 GridSplitterRowDefinition.Height 的大小/代码>

I'm having a hard time with grid splitter. I've bound the RowDefinition.Height dependency property to the clr property of the model as presented below.


    <Grid.RowDefinitions>
        <RowDefinition Height='{Binding Path=Height, Mode=OneWay}' />
        <RowDefinition Height='*' />
    </Grid.RowDefinitions>

This works fine just until the GridSplitter is used. When the height of the row is changed manually with GridSplitter, it replaces the binding with the new fixed size (and removes the binding).

Have you got any ideas or workarounds how to create two rows that would be resizable with GridSplitter but still change their height according to the clr property/binding?

I think the problem is that your source Property Height is of type double and RowDefinition.Height is of type GridLength. Use a converter and it'll work TwoWay

<Grid.RowDefinitions>
    <RowDefinition Height="{Binding Path=Height,
                                    Mode=TwoWay,
                                    Converter={StaticResource DoubleGridLengthConverter}}"/>
    <!--...-->
</Grid.RowDefinitions>

DoubleGridLengthConverter

public class DoubleGridLengthConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return new GridLength((double)value);
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        GridLength gridLength = (GridLength)value;
        return gridLength.Value;
    }
}


Update
Uploaded my sample application here:
http://www.mediafire.com/download.php?pgibb205d65596q

Set the RowDefinition.Height by entering a value in the lower TextBox and resize the RowDefinition.Height with the GridSplitter

这篇关于Wpf GridSplitter 替换 row.height 属性上的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 07:35