其他分享
首页 > 其他分享> > 带有ItemsControl的网格中的Gridsplitter

带有ItemsControl的网格中的Gridsplitter

作者:互联网

我正在尝试制作PropertyGrid自定义控件. PropertyGrid将与Visual Studio中使用的PropertyGrid非常相似.我尝试使用扩展WPF工具包的PropertyGrid,但是必须使用属性指定属性的类别,并且我们需要更改类别运行时.据我所知,属性是不可能的.

因此,我自己制作了PropertyGrid.到目前为止,这是我的代码:

Generic.xaml:

<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

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

物业集团

public class PropertyGroup
{
    public string Name { get; set; }
    public List<PropertyGridItem> Items { get; set; }

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

PropertyGridItem

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中的这段代码:

<local:PropertyGrid ItemsSource="{Binding Path=Groups}" GridColor="#f0f0f0"/>

我的ViewModel后面的代码:

public MainWindow()
{
    InitializeComponent();
    this.DataContext = new ViewModel();
}

ViewModel

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的Grid,如果不可能的话,该GridSplitter适用于理想情况下组的所有行或整个Grid.

解决方法:

我终于找到了解决这个问题的方法.

为了使它起作用,我必须:

>在父ItemsControl上将Grid.IsSharedSizeScope设置为true.
>使用任意名称在名称列上设置SharedSizeGroup属性.
>删除名称列上的星形大小.由于某些原因,第一列和第三列都具有星形大小会导致GridSplitter卡住.
>在GridSplitter上设置固定宽度,将Width设置为2.
>将GridSplitter的ResizeBehaviour设置为PreviousAndNext.

这是所得代码的相关部分:

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

标签:wpftoolkit,custom-controls,wpf,c,itemscontrol
来源: https://codeday.me/bug/20191122/2059366.html