首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有ItemsControl的网格中的网格划分器

具有ItemsControl的网格中的网格划分器
EN

Stack Overflow用户
提问于 2013-12-10 10:13:00
回答 2查看 1.9K关注 0票数 3

我正在尝试做一个PropertyGrid自定义控件。PropertyGrid将非常类似于Visual中使用的PropertyGrid。我试过使用Extended的PropertyGrid,但是您必须使用属性指定属性的类别,我们需要更改类别运行时。据我所知,有属性是不可能的。

所以我自己做PropertyGrid。到目前为止,这是我的代码:

Generic.xaml:

代码语言:javascript
复制
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:HomeMadePropertyGrid"
                    xmlns:System="clr-namespace:System;assembly=mscorlib">

    <BooleanToVisibilityConverter x:Key="BoolToVisConverter"></BooleanToVisibilityConverter>
    <SolidColorBrush x:Key="GlyphBrush" Color="#444" />
    <ControlTemplate x:Key="toggleButtonTemplate" TargetType="ToggleButton">
        <Grid Width="15" Height="13" Background="Transparent">
            <Path x:Name="ExpandPath" Fill="{StaticResource GlyphBrush}" Data="M 4 0 L 8 4 L 4 8 Z" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="1,1,1,1" />
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsChecked" Value="True">
                <Setter Property="Data" TargetName="ExpandPath" Value="M 0 4 L 8 4 L 4 8 Z"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
    <Style x:Key="toggleButtonStyle" TargetType="ToggleButton">
        <Setter Property="Template" Value="{StaticResource toggleButtonTemplate}" />
    </Style>

    <Style TargetType="{x:Type local:PropertyGrid}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:PropertyGrid}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <ItemsControl ItemsSource="{TemplateBinding ItemsSource}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <VirtualizingStackPanel/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Background="{Binding GridColor, RelativeSource={RelativeSource AncestorType=local:PropertyGrid}}">
                                        <StackPanel Orientation="Horizontal">
                                            <ToggleButton x:Name="toggleButton" Height="20" Width="20" Style="{StaticResource toggleButtonStyle}"/>
                                            <TextBlock Text="{Binding Name}" FontWeight="Bold"></TextBlock>
                                        </StackPanel>
                                        <ItemsControl ItemsSource="{Binding Items}">
                                            <ItemsControl.ItemTemplate>
                                                <DataTemplate>
                                                    <Grid Visibility="{Binding ElementName=toggleButton, Path=IsChecked, Converter={StaticResource BoolToVisConverter}}" 
                                                          Grid.IsSharedSizeScope="True">

                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="*"/>
                                                            <ColumnDefinition Width="Auto" />
                                                            <ColumnDefinition Width="*"/>
                                                        </Grid.ColumnDefinitions>

                                                        <Border BorderThickness="1" BorderBrush="{Binding GridColor, RelativeSource={RelativeSource AncestorType=local:PropertyGrid}}">
                                                            <TextBlock Background="White" Text="{Binding Path=Name}"/>
                                                        </Border>
                                                        <GridSplitter Width="1" 
                                                                      Grid.RowSpan="4" Grid.Column="1" 
                                                                      HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                                                                      Background="{Binding GridColor, RelativeSource={RelativeSource AncestorType=local:PropertyGrid}}"/>
                                                        <Border Grid.Column="2" BorderThickness="1" BorderBrush="{Binding GridColor, RelativeSource={RelativeSource AncestorType=local:PropertyGrid}}">
                                                            <ContentPresenter Grid.Column="2" Content="{Binding Value}">
                                                                <ContentPresenter.Resources>
                                                                    <DataTemplate DataType="{x:Type System:String}">
                                                                        <TextBox Text="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}" 
                                                                             BorderThickness="0"/>
                                                                    </DataTemplate>
                                                                    <DataTemplate DataType="{x:Type System:Int32}">
                                                                        <TextBox Text="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}" 
                                                                             TextAlignment="Right"
                                                                             BorderThickness="0"/>
                                                                    </DataTemplate>
                                                                    <DataTemplate DataType="{x:Type System:Double}">
                                                                        <TextBox Text="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}" 
                                                                             TextAlignment="Right"
                                                                             BorderThickness="0"/>
                                                                    </DataTemplate>
                                                                    <DataTemplate DataType="{x:Type System:Boolean}">
                                                                        <CheckBox IsChecked="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}"
                                                                                  HorizontalAlignment="Center"/>
                                                                    </DataTemplate>
                                                                </ContentPresenter.Resources>
                                                            </ContentPresenter>
                                                        </Border>
                                                    </Grid>
                                                </DataTemplate>
                                            </ItemsControl.ItemTemplate>
                                        </ItemsControl>
                                    </StackPanel>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

PropertyGrid.cs

代码语言:javascript
复制
public class PropertyGrid : ItemsControl
{
    public Brush GridColor
    {
        get { return (Brush)GetValue(GridColorProperty); }
        set { SetValue(GridColorProperty, value); }
    }

    public static readonly DependencyProperty GridColorProperty =
        DependencyProperty.Register("GridColor", typeof(Brush), typeof(PropertyGrid), new UIPropertyMetadata(new SolidColorBrush(Colors.Transparent)));

    static PropertyGrid()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(PropertyGrid), new FrameworkPropertyMetadata(typeof(PropertyGrid)));
    }
}

PropertyGroup

代码语言:javascript
复制
public class PropertyGroup
{
    public string Name { get; set; }
    public List<PropertyGridItem> Items { get; set; }

    public PropertyGroup()
    {
        Items = new List<PropertyGridItem>();
        Name = "";
    }
}

PropertyGridItem

代码语言:javascript
复制
public class PropertyGridItem
{
    public string Name { get; set; }
    public object Value { get; set; }

    public PropertyGridItem(string propertyName, object propertyValue)
    {
        Name = propertyName;
        Value = propertyValue;
    }
}

下面是我的MainWindow.xaml中的代码:

代码语言:javascript
复制
<local:PropertyGrid ItemsSource="{Binding Path=Groups}" GridColor="#f0f0f0"/>

我的ViewModel背后的代码:

代码语言:javascript
复制
public MainWindow()
{
    InitializeComponent();
    this.DataContext = new ViewModel();
}

The ViewModel

代码语言:javascript
复制
public class ViewModel
{
    public List<PropertyGroup> Groups { get; set; }

    public ViewModel()
    {
        Groups = new List<PropertyGroup>();

        PropertyGroup group1 = new PropertyGroup();
        group1.Name = "Group1";
        group1.Items.Add(new PropertyGridItem("Item1", "test"));
        group1.Items.Add(new PropertyGridItem("Item2", 300));
        group1.Items.Add(new PropertyGridItem("Item3", true));
        group1.Items.Add(new PropertyGridItem("Item4", 5.2));
        Groups.Add(group1);

        PropertyGroup group2 = new PropertyGroup();
        group2.Name = "Group2";
        group2.Items.Add(new PropertyGridItem("Item1", "test"));
        group2.Items.Add(new PropertyGridItem("Item2", 300));
        group2.Items.Add(new PropertyGridItem("Item3", true));
        group2.Items.Add(new PropertyGridItem("Item4", 5.2));
        Groups.Add(group2);
    }
}

我遇到的问题是,每个行都应用GridSplitter。我希望将GridSplitter应用于组的所有行。我理解这是因为我为每个项目创建了一个新的网格。要使附加的属性工作,这些项必须是Grid的直接子元素。DataGrid也不是一个选项,因为GridSplitter只在列标题之间可用。

因此,长话短说:如果不可能的话,我如何在ItemsControl中使用网格和应用于理想的组或整个网格的所有行的GridSplitter。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-26 15:30:20

我终于找到了解决这个问题的办法。

为了让这件事起作用,我不得不:

  • 在父Grid.IsSharedSizeScope上将ItemsControl设置为true。
  • 使用任意名称在name列上设置SharedSizeGroup属性。
  • 删除name列上的星号大小。由于某种原因,第一列和第三列都有星号大小,导致GridSplitter卡住了。
  • 在GridSplitter上设置一个固定宽度,我将宽度设置为2。
  • 将GridSplitter的GridSplitter设置为PreviousAndNext。

下面是生成代码的相关部分:

代码语言:javascript
复制
 <ItemsControl ItemsSource="{Binding Items}" Grid.IsSharedSizeScope="True">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
                <Grid Visibility="{Binding ElementName=toggleButton, Path=IsChecked, Converter={StaticResource BoolToVisConverter}}" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="nameColumn"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Border Grid.Column="0"  Style="{StaticResource BodyPropertyGrid_CellBorder}">
                    <TextBlock Text="{Binding Path=Name}"/>
                </Border>
                <GridSplitter Grid.Column="1" Width="2"
                              ResizeBehavior="PreviousAndNext"
                              Style="{StaticResource BodyPropertyGridSplitter}"/>
                <Border Grid.Column="2" Style="{StaticResource BodyPropertyGrid_CellBorder}">
                    <ContentControl Content="{Binding}" 
                                    ContentTemplateSelector="{StaticResource propertyItemTemplateSelector}"/>
                </Border>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
票数 5
EN

Stack Overflow用户

发布于 2013-12-10 10:37:11

不幸的是,您没有为我们提供一段很好的简单代码,它只演示了您的问题,而且我没有时间让它在我的测试项目中编译和运行,所以我只能给您建议,而不能给出经过测试的解决方案。下一次,请花时间展示一个代码示例,我们可以复制并粘贴到一个项目中。

如果使用GridSplitter连接所有行Grid,则可以获得“拉伸” Attached Property

代码语言:javascript
复制
<Grid Visibility="{Binding ElementName=toggleButton, Path=IsChecked, Converter=
    {StaticResource BoolToVisConverter}}" Grid.IsSharedSizeScope="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" SharedSizeGroup="FirstColumn" />
        <ColumnDefinition Width="Auto" SharedSizeGroup="GridSplitterColumn" />
        <ColumnDefinition Width="*" SharedSizeGroup="LastColumn" />
    </Grid.ColumnDefinitions>
    ...
</Grid>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20491585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档