GridSplitter - привязка к maxWidth не работает

Мне нравится делать привязку к MaxWidth Grid-Column, которую я использую с GridSplitter. Если я использую MaxWidth, жестко закодированный внутри XAML, все работает нормально, но если я использую привязку к коду программной части, это больше не будет работать. Сама привязка к ширине работает (можно проверить нажатием кнопки).

Привязка не работает:

<ColumnDefinition Name="gs_c1" MaxWidth="{Binding Path=GsC1_maxWidth, Mode=TwoWay, Converter={StaticResource DoubleGridLengthConverter}}" Width="{Binding Path=GsC1_width, Mode=TwoWay, Converter={StaticResource DoubleGridLengthConverter}}"/>

MaxWidth работает:

<ColumnDefinition Name="gs_c3" MaxWidth="150" Width="{Binding Path=GsC3_width, Mode=TwoWay, Converter={StaticResource DoubleGridLengthConverter}}"/>

XAML:

<Window x:Class="GridSplitter_02.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:GridSplitter_02"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="1525">
    <Grid>
        <Grid.Resources>
            <local:DoubleGridLengthConverter x:Key="DoubleGridLengthConverter"/>
        </Grid.Resources>

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="20" />
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>            

            <Button Grid.Row="0" Click="Button_Click">Reset width</Button>
            <Grid Grid.Row="1" Margin="15">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Name="gs_c0" Width="{Binding Path=GsC0_width, Mode=TwoWay, Converter={StaticResource DoubleGridLengthConverter}}"/>
                    <!--Binding to the maxWidth does not work-->
                    <ColumnDefinition Name="gs_c1" MaxWidth="{Binding Path=GsC1_maxWidth, Mode=TwoWay, Converter={StaticResource DoubleGridLengthConverter}}" Width="{Binding Path=GsC1_width, Mode=TwoWay, Converter={StaticResource DoubleGridLengthConverter}}"/>
                    <ColumnDefinition Width="5" />
                    <ColumnDefinition Name="gs_c2" Width="{Binding Path=GsC2_width, Mode=TwoWay, Converter={StaticResource DoubleGridLengthConverter}}"/>
                    <!--MaxWidth is working here-->
                    <ColumnDefinition Name="gs_c3" MaxWidth="150" Width="{Binding Path=GsC3_width, Mode=TwoWay, Converter={StaticResource DoubleGridLengthConverter}}"/>
                    <ColumnDefinition Width="5" />
                    <ColumnDefinition Name="gs_c4" Width="{Binding Path=GsC4_width, Mode=TwoWay, Converter={StaticResource DoubleGridLengthConverter}}"/>
                </Grid.ColumnDefinitions>

                <Rectangle Grid.Column="0" Fill="Blue" />
                <Grid Grid.Column="1">
                    <Rectangle  Fill="LightBlue" />
                    <TextBlock>Text</TextBlock>
                </Grid>
                <GridSplitter Grid.Column="2" Background="Black" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="5" />

                <Rectangle Grid.Column="3" Fill="Red" />
                <Grid Grid.Column="4">
                    <Rectangle  Fill="LightCoral" />
                    <TextBlock>Text</TextBlock>
                </Grid>

                <GridSplitter Grid.Column="5" Background="Black" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="5" />

                <Rectangle Grid.Column="6" Fill="Gray" />
            </Grid>
        </Grid>
    </Grid>
</Window>

Код программной части:

namespace GridSplitter_02
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        #region Width
        public double GsC0_width
        {
            get
            {
                return _gsC0_width;
            }
            set
            {
                _gsC0_width = value;
                CalcSpace();
                OnPropertyChanged("GsC0_width");
            }
        }
        private double _gsC0_width;


        public double GsC1_width
        {
            get
            {
                return _gsC1_width;
            }
            set
            {
                _gsC1_width = value;
                CalcSpace();
                OnPropertyChanged("GsC1_width");
            }
        }
        private double _gsC1_width;


        public double GsC2_width
        {
            get
            {
                return _gsC2_width;
            }
            set
            {
                _gsC2_width = value;
                CalcSpace();
                OnPropertyChanged("GsC2_width");
            }
        }
        private double _gsC2_width;


        public double GsC3_width
        {
            get
            {
                return _gsC3_width;
            }
            set
            {
                _gsC3_width = value;
                CalcSpace();
                OnPropertyChanged("GsC3_width");
            }
        }
        private double _gsC3_width;


        public double GsC4_width
        {
            get
            {
                return _gsC4_width;
            }
            set
            {
                _gsC4_width = value;
                OnPropertyChanged("GsC4_width");
            }
        }
        private double _gsC4_width;
        #endregion


        #region maxWidth
        public double GsC0_maxWidth
        {
            get
            {
                return _gsC0_maxWidth;
            }
            set
            {
                _gsC0_maxWidth = value;
                OnPropertyChanged("GsC0_maxWidth");
            }
        }
        private double _gsC0_maxWidth;


        public double GsC1_maxWidth
        {
            get
            {
                return _gsC1_maxWidth;
            }
            set
            {
                _gsC1_maxWidth = value;
                OnPropertyChanged("GsC1_maxWidth");
            }
        }
        private double _gsC1_maxWidth = 150;


        public double GsC2_maxWidth
        {
            get
            {
                return _gsC2_maxWidth;
            }
            set
            {
                _gsC2_maxWidth = value;
                OnPropertyChanged("GsC2_maxWidth");
            }
        }
        private double _gsC2_maxWidth;


        public double GsC3_maxWidth
        {
            get
            {
                return _gsC3_maxWidth;
            }
            set
            {
                _gsC3_maxWidth = value;
                OnPropertyChanged("GsC3_maxWidth");
            }
        }
        private double _gsC3_maxWidth;


        public double GsC4_maxWidth
        {
            get
            {
                return _gsC4_maxWidth;
            }
            set
            {
                _gsC4_maxWidth = value;
                OnPropertyChanged("GsC4_maxWidth");
            }
        }
        private double _gsC4_maxWidth;
        #endregion



        private double _maxSpace = 1500;
        public MainWindow()
        {
            InitializeComponent();

            SetWidth();

            this.DataContext = this;
        }


        private void CalcSpace()
        {
            GsC4_width = _maxSpace - GsC0_width - GsC1_width - GsC2_width - GsC3_width;
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            SetWidth();
        }


        private void SetWidth()
        {
            GsC0_width = 15;
            GsC1_width = 15;
            GsC1_maxWidth = 150;

            GsC2_width = 500;
            GsC3_width = 100;
            GsC3_maxWidth = 400;

            CalcSpace();
        }



        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

Конвертер:

namespace GridSplitter_02
{
    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;
        }
    }
}

person MUT    schedule 29.12.2016    source источник


Ответы (1)


Я нашел проблему. Преобразователь значений не работает с Maxwidth. Если убрать преобразователь, то все работает.

Рабочий код:

<ColumnDefinition Name="gs_c1" MaxWidth="{Binding Path=GsC1_maxWidth, Mode=TwoWay}" Width="{Binding Path=GsC1_width, Mode=TwoWay, Converter={StaticResource DoubleGridLengthConverter}}"/>
person MUT    schedule 30.12.2016