赞
踩
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。。。。。
}
}
在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>
设置对应属性的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)));
}
}
使用方式:
[EditorAttribute(typeof(ImageEditor), typeof(ImageEditor))]
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。