Wpf GridSplitter replaces binding on row.height property

纵然是瞬间 提交于 2019-12-22 04:28:27

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/5259729/wpf-gridsplitter-replaces-binding-on-row-height-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!