当前位置:   article > 正文

基于WpfToolKit 自定义属性编辑_xceed.wpf.toolkit

xceed.wpf.toolkit

Xceed.Wpf.Toolkit 中对 ITypeEditor进行了封装封装后的类为TypeEditor<T> ,我们可以通过这个类来达到自定义的目的。 属性栏中显示成什么样,可以通过自己定义一个相应的控件来实现,或者使用现有的控件。示例如下

我这里是通过继承Button来实现的

    /// <summary>
    /// ImageSelectControl.xaml 的交互逻辑
    /// </summary>
    public partial class ImageSelectButton : Button
    {
        static ImageSelectButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageSelectButton), new FrameworkPropertyMetadata(typeof(ImageSelectButton)));
        }

        public ImageSelectButton()
        {
            InitializeComponent();
            DataContext = this;
            this.Click += ImageSelectButton_Click;
        }

        public string SelectPath
        {
            get { return (string)GetValue(SelectPathProperty); }
            set { SetValue(SelectPathProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Path.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectPathProperty =
            DependencyProperty.Register("SelectPath", typeof(string), typeof(ImageSelectButton), new PropertyMetadata(""));





        private void ImageSelectButton_Click(object sender, RoutedEventArgs e)
        {
        //Todo。。。。。
        }

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

Xaml里面可以自定义显示成什么样
Xaml如下:

    <Button.Resources>
        <Style  TargetType="{x:Type local:ImageSelectButton}">
            <Setter Property="Background" Value="White" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Padding"  Value="2,0,0,0" />
            <Setter Property="HorizontalContentAlignment" Value="Left" />
            <Setter Property="VerticalContentAlignment"  Value="Center" />
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <Setter Property="MinHeight" Value="22" />
            <Setter Property="IsTabStop" Value="True" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid SnapsToDevicePixels="True">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>

                                <Border Background="{TemplateBinding Background}"
                             BorderBrush="{TemplateBinding BorderBrush}"
                             BorderThickness="{TemplateBinding BorderThickness}"
                             Padding="{TemplateBinding Padding}"
                             SnapsToDevicePixels="True">
                                    <ContentPresenter Content="{Binding SelectPath, RelativeSource={RelativeSource TemplatedParent}}"
                                          ContentTemplate="{TemplateBinding ContentTemplate}"
                                          ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
                                </Border>

                                <exitToolkit:ButtonChrome x:Name="ToggleButtonChrome"
                                          Grid.Column="1"
                                          CornerRadius="0,2.75,2.75,0"
                                          RenderEnabled="{Binding IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ImageSelectButton}}"
                                          RenderMouseOver="{TemplateBinding IsMouseOver}">
                                    <Grid x:Name="arrowGlyph" IsHitTestVisible="False" Grid.Column="1" Margin="5">
                                        <TextBlock Text="....."/>
                                    </Grid>
                                </exitToolkit:ButtonChrome>
                            </Grid>

                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Foreground" Value="Gray" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Button.Resources>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

设置对应属性的Attribute就可以

    public class ImageEditor : TypeEditor<ImageSelectButton>
    {
        protected override void SetControlProperties()
        {

        }
        protected override void SetValueDependencyProperty()
        {
            ValueProperty = ImageSelectButton.SelectPathProperty;
        }
    }

    public class PropertyGridEditorImageSelect : ImageSelectButton
    {
        static PropertyGridEditorImageSelect()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(PropertyGridEditorImageSelect), new FrameworkPropertyMetadata(typeof(PropertyGridEditorImageSelect)));
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

使用方式:

 [EditorAttribute(typeof(ImageEditor), typeof(ImageEditor))]
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/270707
推荐阅读
相关标签
  

闽ICP备14008679号