PropertyGrid自定义控件使用详解

2019-12-30 17:48:39于丽

该控件使用的资源字典如下:

 


<ResourceDictionary
  xmlns="http://www.easck.com/winfx/2006/xaml/presentation"
  xmlns:x="http://www.easck.com/winfx/2006/xaml"
  xmlns:d="http://www.easck.com/expression/blend/2008"
  xmlns:dxe="http://www.easck.com/winfx/2008/xaml/editors"
  xmlns:dxg="http://www.easck.com/winfx/2008/xaml/grid"
  xmlns:dxprg="http://www.easck.com/winfx/2008/xaml/propertygrid"
  xmlns:local="clr-namespace:PropertyGridDemo.PropertyGridControl"
  xmlns:mc="http://www.easck.com/markup-compatibility/2006"
  mc:Ignorable="d">

  <local:DynamicallyAssignDataEditorsTemplateSelector x:Key="DynamicallyAssignDataEditorsTemplateSelector" />
  <local:DataEditorsViewModel x:Key="DemoDataProvider" />

  <DataTemplate x:Key="DescriptionTemplate">
    <RichTextBox
      x:Name="descriptionRichTextBox"
      MinWidth="150"
      HorizontalContentAlignment="Stretch"
      Background="Transparent"
      BorderThickness="0"
      Foreground="{Binding Path=(TextElement.Foreground), RelativeSource={RelativeSource TemplatedParent}}"
      IsReadOnly="True"
      IsTabStop="False" />
  </DataTemplate>
  <DataTemplate x:Key="descriptionTemplate">
    <RichTextBox
      x:Name="descriptionRichTextBox"
      MinWidth="150"
      HorizontalContentAlignment="Stretch"
      Background="Transparent"
      BorderThickness="0"
      Foreground="{Binding Path=(TextElement.Foreground), RelativeSource={RelativeSource TemplatedParent}}"
      IsReadOnly="True"
      IsTabStop="False" />
  </DataTemplate>

  <!-- 设置控件的全局样式和数据绑定 -->
  <Style x:Key="DynamicallyAssignDataEditorsPropertyDefinitionStyle" TargetType="dxprg:PropertyDefinition">
    <Setter Property="Path" Value="{Binding Name}" />
    <!--<Setter Property="Header" Value="{Binding Converter={StaticResource PropertyDescriptorToDisplayNameConverter}}"/>-->
    <Setter Property="Description" Value="{Binding}" />
    <Setter Property="DescriptionTemplate" Value="{StaticResource descriptionTemplate}" />
  </Style>
  <Style x:Key="DescriptionContainerStyle" TargetType="dxprg:PropertyDescriptionPresenterControl">
    <Setter Property="ShowSelectedRowHeader" Value="False" />
    <Setter Property="MinHeight" Value="70" />
  </Style>

  <Style TargetType="Slider">
    <Setter Property="Margin" Value="2" />
  </Style>
  <Style TargetType="dxe:ComboBoxEdit">
    <Setter Property="IsTextEditable" Value="False" />
    <Setter Property="ApplyItemTemplateToSelectedItem" Value="True" />
    <Setter Property="Margin" Value="2" />
  </Style>

  <!-- 测试直接从DataTemplate获取控件 -->
  <DataTemplate x:Key="SliderTemplate" DataType="local:SliderExtend">
    <!--<dxprg:PropertyDefinition>
      <dxprg:PropertyDefinition.CellTemplate>-->
    <!--<DataTemplate>-->
    <StackPanel x:Name="Root">
      <Slider
        Maximum="{Binding Path=Max}"
        Minimum="{Binding Path=Min}"
        Value="{Binding Path=Value}" />
      <TextBlock Text="{Binding Path=Value}" />
    </StackPanel>
    <!--</DataTemplate>-->
    <!--</dxprg:PropertyDefinition.CellTemplate>
    </dxprg:PropertyDefinition>-->
  </DataTemplate>

  <DataTemplate x:Key="ComboBoxEditItemTemplate" DataType="Tuple">
    <TextBlock
      Height="20"
      Margin="5,3,0,0"
      VerticalAlignment="Center"
      Text="{Binding Item1}" />
  </DataTemplate>
</ResourceDictionary>