GridSplitter - 绑定到maxWidth不起作用

时间:2016-12-29 20:50:08

标签: c# wpf data-binding gridsplitter

我喜欢使用GridSplitter对Grid-Column的MaxWidth进行绑定。 如果我在XAML中使用硬编码的MaxWidth一切正常,但如果我使用绑定到代码隐藏它将不再工作。绑定到宽度本身是有效的(可以通过按下按钮进行测试)。

绑定不起作用:

<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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我发现了问题。 值转换器不能通过Maxwidth工作。如果我删除转换器,它可以正常工作。

工作代码:

    <groovy>
  ant.mkdir(dir:"target")


  new File("target/UpsertCheckDeals.csv").withWriter {
    new File("C:/Users/alon\Documents\CheckDealBeforeUpsert.csv").splitEachLine(",") { Customer__c, Name__c, Deal__c, Amount__c, CheckCount__c, Discount__c ->
       it.println "${Customer__c},${Name__c},${CheckCount__c},${Deal__c},${Amount__c},${Discount__c},${Customer__c}-${Deal__c }"
    }
  }

</groovy>