为什么GridSplitter不工作?怎么找出来?

时间:2014-06-20 10:02:23

标签: c# wpf

我创建了一个自定义ItemControl,使用Grid来显示项目。每个其他项目都是GridSplitter,因此用户可以更改行的大小。我确保GridSplitters直接添加到网格中,并且不在ContentPresenters中,您可以在下面的Snoop屏幕截图中看到。但是,它们仍然无法正常工作。如果我将鼠标移动到它上方,则会显示鼠标右键,但我无法拖动分割器 - 它不会移动。你能帮我解释一下为什么分离器不动了吗?下面的代码只是问题的演示,它不是我将要使用的真实代码。

这就是应用程序的样子,显示GridSlitters的Snoop窗口直接在Grid中。 enter image description here

这是我的自定义ItemsControl。当项目是GridSplitter时,它不会创建容器。

public class GridItemsControl : ItemsControl
{
    protected override bool IsItemItsOwnContainerOverride(object item)
    {
        return (item is GridSplitter);
    }

    protected override bool ShouldApplyItemContainerStyle(DependencyObject container, object item)
    {
        return !(item is GridSplitter);
    }
}

Xaml如何使用它。

<local:GridItemsControl x:Name="ItemsControl" ItemsSource="{Binding Items}" >
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid IsItemsHost="True" >
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemContainerStyle>
        <Style TargetType="{x:Type ContentPresenter}">
            <Setter Property="Grid.Row" Value="{Binding Row}" />
            <Setter Property="Grid.Column" Value="{Binding Column}" />
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Border BorderThickness="2" BorderBrush="Black">
                            <TextBlock Foreground="DarkGray" Text="{Binding Content}" Margin="4" />
                        </Border>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ItemsControl.ItemContainerStyle>
</local:GridItemsControl>

viemodel。

public class MainViewModel
{
    public ObservableCollection<object> Items { get; set; }

    public MainViewModel()
    {
        Items = new ObservableCollection<object>();
    }
}

这就是我创建填充视图模型的方法。

   public partial class MainWindow : Window
   {
        private MainViewModel _viewModel = new MainViewModel();

        public MainWindow()
        {
            InitializeComponent();

            for (int i = 0; i < 10; i++)
            {
                _viewModel.Items.Add(new GridItem
                {
                    Row = i,
                    Column = 0,
                    Content = "item" + i
                });

                var splitter = new GridSplitter
                {
                    Height = 5,
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    VerticalAlignment = VerticalAlignment.Bottom,
                    Background = Brushes.CornflowerBlue,
                    ResizeBehavior = GridResizeBehavior.PreviousAndNext,
                    ResizeDirection = GridResizeDirection.Rows
                };
                splitter.SetValue(Grid.RowProperty, i);
                splitter.SetValue(Grid.ColumnProperty, 0);

                _viewModel.Items.Add(splitter);
            }

            DataContext = _viewModel;
        }
 }

public class GridItem
{
    public object Content { get; set; }
    public int Row { get; set; }
    public int Column { get; set; }
}

为什么不起作用。如何进一步调查?

1 个答案:

答案 0 :(得分:1)

尝试将此tow属性添加到拆分器声明ResizeBehavior和ResizeDirection。

var splitter = new GridSplitter
            {
                Height = 5,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Bottom,
                Background = Brushes.CornflowerBlue,
                ResizeBehavior = GridResizeBehavior.PreviousAndNext,
                ResizeDirection = GridResizeDirection.Rows,
            };