WPF网格 - 按特定顺序调整列大小

时间:2015-09-04 06:57:24

标签: wpf blend gridsplitter

我的WPF网格有问题。需要一些帮助。

我有一个分为两列的网格(例如" col1"" col2"从左到右)。此网格绑定到窗口边缘,没有填充:

| --- col1 --- | --- col2 --- | ←右窗边框

如何使这些列按特定顺序与应用程序窗口一起调整大小?

我的意思是: 当我从右边框缩小窗口时,我需要col2来调整大小。当col2达到MinWidth时 - 然后col1开始缩小(我继续移动窗口的右边框)。

是否可以定义列更改大小的顺序? 或者我可能需要除了网格以外的其他东西吗?

感谢。

1 个答案:

答案 0 :(得分:0)

了解如何处理事件并解雇自己是了解C#的基石,所以我强烈建议你阅读它。话虽这么说,这将让你开始,虽然我不确定这正是你想要的...请参阅下面的示例代码。我留下编写代码,当窗口宽度增加时作为练习给你。祝你好运=)。

在我的MainWindow上,我设置Height="250" Width="600",其中的网格有两列......

 <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition x:Name="_col1" MinWidth="200" />
            <ColumnDefinition x:Name="_col2" MinWidth="150"/>
        </Grid.ColumnDefinitions>

        <TextBlock x:Name="_txtCol1Width"
                   Grid.Column="0" 
                   Background="Crimson" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />

        <TextBlock x:Name="_txtCol2Width"
                   Grid.Column="1" 
                   Background="Turquoise" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  />
 </Grid>

X

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
            this.SizeChanged += MainWindow_SizeChanged;
        }

        void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (this.IsLoaded && e.WidthChanged)
            {
                double changeInWindowWidth = e.NewSize.Width - e.PreviousSize.Width;

                if (changeInWindowWidth < 0)
                {
                    if (_col2.ActualWidth + changeInWindowWidth >= _col2.MinWidth)
                    {
                        // col 2 has not yet reached its minimum (decrease col2, no change for col1)
                        _col2.Width = new GridLength(_col2.ActualWidth + changeInWindowWidth, GridUnitType.Pixel);
                        _col1.Width = new GridLength(_col1.ActualWidth + 0, GridUnitType.Pixel);
                    }
                    else if (_col1.ActualWidth + changeInWindowWidth >= _col1.MinWidth)
                    {
                        // col 2 has reached its minimum, but col1 has not (decrease col1, no change for col2)
                        _col1.Width = new GridLength(_col1.ActualWidth + changeInWindowWidth, GridUnitType.Pixel);
                        _col2.Width = new GridLength(_col2.ActualWidth + 0, GridUnitType.Pixel);
                    }
                    else
                    {
                        // both columns have reached their minimum, so decrease width of both equally
                        _col1.MinWidth = _col1.ActualWidth + 0.5 * changeInWindowWidth;
                        _col2.MinWidth = _col2.ActualWidth + 0.5 * changeInWindowWidth;
                        _col1.Width = new GridLength(_col1.MinWidth, GridUnitType.Pixel);
                        _col2.Width = new GridLength(_col2.MinWidth, GridUnitType.Pixel);
                    }
                }
                else
                {
                    // todo: handle window width increased ... 

                }

                UpdateTexts();
            }
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            UpdateTexts();
        }

        private void UpdateTexts()
        {
            _txtCol1Width.Text = String.Format("column {0}\nActualWidth: {1}\n(MinWidth: {2})", 1, _col1.ActualWidth, _col1.MinWidth);
            _txtCol2Width.Text = String.Format("column {0}\nActualWidth: {1}\n(MinWidth: {2})", 2, _col2.ActualWidth, _col2.MinWidth);
        }
    }