GridSplitter不存在

时间:2014-12-10 01:58:00

标签: wpf

我在后面的代码中使用它来测试如何以编程方式添加网格分割器。 (我知道,不要使用代码 - 但这是我需要的极少数情况之一。(我认为))

public partial class ContainerView : Window, IContainerView
{
    [ImportingConstructor]
    public ContainerView()
    {
        InitializeComponent();

        SetUp();
    }

    public void SetUp()
    {
        _grid = new Grid();

        //Single column/single row
        _grid.ColumnDefinitions.Add(new ColumnDefinition());
        _grid.ColumnDefinitions.Add(new ColumnDefinition());
        _grid.ColumnDefinitions.Add(new ColumnDefinition());

        _grid.RowDefinitions.Add(new RowDefinition());
        _grid.RowDefinitions.Add(new RowDefinition());

        var button1 = new Button();
        button1.Content = "Btn 1";
        button1.Margin = new Thickness(5);
        Grid.SetRow(button1, 0);
        Grid.SetColumn(button1, 0);

        var button2 = new Button();
        button2.Content = "Btn 2";
        button2.Margin = new Thickness(5);
        Grid.SetRow(button2, 1);
        Grid.SetColumn(button2, 2);

        _grid.Children.Add(button1);
        _grid.Children.Add(button2);

        var splitterV = new GridSplitter();
        Grid.SetRowSpan(splitterV, _grid.RowDefinitions.Count);
        splitterV.VerticalAlignment = VerticalAlignment.Stretch;
        splitterV.HorizontalAlignment = HorizontalAlignment.Right;
        splitterV.ShowsPreview = true;
        splitterV.Background = Brushes.Black;
        Width = 5;

        _grid.Children.Add(splitterV);
        Grid.SetColumn(splitterV, 1);

        Content = _grid;
    }

我可以看到两个按钮,但中间列是空的。 GridSplitter未显示。我做错了什么?

1 个答案:

答案 0 :(得分:1)

你是绝对正确的,但你设置的是窗口的宽度而不是GridSpitter的宽度和宽度。你也必须给它ResizeBehaviour。

而不是:

Width = 5;

设置GridSpitter的宽度和宽度它的ResizeBehavior为:

splitterV.ResizeBehavior = GridResizeBehavior.PreviousAndNext;
splitterV.Width = 5;