DevExpress WPF PropertyGridControl 行定义的Header必填项标记

396 阅读1分钟

1. 查看PropertyGridControl的样式

通过 DevExpress WPF Themes工具查看PropertyGridControlVisualTree结构,可以看到 PropertyGridControl行的Header部分包含PART_Indent,PART_CheckBox,PART_ContentPresenter三部分内容,而PART_ContentPresenter中的元素默认是一个TextBlock

image.png

2. 自定义 PropertyGridControlPropertyDefinition.HeaderTemplate样式

2.1 HeaderTemplate

<dxprg:PropertyDefinition>
    <dxprg:PropertyDefinition.HeaderTemplate>
        <DataTemplate>
            <Border>
                <StackPanel Orientation="Horizontal">
                    <TextBlock>
                        <Run Text="{Binding Header}"/>
                        <Run Text="{Binding Header,Converter={StaticResource MandatoryAsteriskConverter}}" Foreground="Red" FontWeight="Bold"/>
                    </TextBlock>
                    <!--<Label Content="{Binding Header,Converter={StaticResource MandatoryAsteriskConverter}}" Foreground="Red" 
                           FontWeight="Bold" VerticalContentAlignment="Center" Margin="5,0,0,0"/>-->
                </StackPanel>
            </Border>
        </DataTemplate>
    </dxprg:PropertyDefinition.HeaderTemplate>
</dxprg:PropertyDefinition>

2.2 MandatoryAsteriskConverter

using System;
using System.Globalization;
using System.Windows.Data;

namespace TestDemo.Converters
{
    public class MandatoryAsteriskConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(value?.ToString() == "MenuCustomizations" ||
                value?.ToString() == "PropertyDefinitionTemplate")
            {
                return " *";
            }
            return string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

3. 完整Demo

3.1 MainWindow.xaml

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dxprg="http://schemas.devexpress.com/winfx/2008/xaml/propertygrid" x:Class="TestDemo.MainWindow"
    xmlns:converters ="clr-namespace:TestDemo.Converters"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <converters:MandatoryAsteriskConverter x:Key="MandatoryAsteriskConverter"/>
        </Grid.Resources>
        <dxprg:PropertyGridControl x:Name="grid" SelectedObject="{Binding ElementName=grid}">
            <dxprg:PropertyDefinition>
                <dxprg:PropertyDefinition.HeaderTemplate>
                    <DataTemplate>
                        <Border>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock>
                                    <Run Text="{Binding Header}"/>
                                    <Run Text="{Binding Header,Converter={StaticResource MandatoryAsteriskConverter}}" Foreground="Red" FontWeight="Bold"/>
                                </TextBlock>
                                <!--<Label Content="{Binding Header,Converter={StaticResource MandatoryAsteriskConverter}}" Foreground="Red" 
                                       FontWeight="Bold" VerticalContentAlignment="Center" Margin="5,0,0,0"/>-->
                            </StackPanel>
                        </Border>
                    </DataTemplate>
                </dxprg:PropertyDefinition.HeaderTemplate>
            </dxprg:PropertyDefinition>
            <dxprg:CategoryDefinition>
                <dxprg:CategoryDefinition.HeaderTemplate>
                    <DataTemplate>
                        <Border Background="Green">
                            <TextBlock Text="{Binding Header}"/>
                        </Border>
                    </DataTemplate>
                </dxprg:CategoryDefinition.HeaderTemplate>
            </dxprg:CategoryDefinition>
        </dxprg:PropertyGridControl>
    </Grid>
</Window>

3.2 MainWindow.cs

using System.Windows;

namespace TestDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

4 运行效果

image.png