如何创建一个自定义按钮控件,它在按钮构造函数中获取path的参数,以便我可以在整个项目中重用控件?
这就是我通常创建按钮的方式....
<Grid>
<Button Style="{DynamicResource ButtonPathStyle}" Width="24" Height="24">
<Path Fill="Gray" Data="M50,50 L100,100 L150,50" Stretch="Uniform" />
</Button>
</Grid>
但是我想创建一个自定义按钮,这样我就可以创建这样的按钮以及自定义样式表
<Grid>
<PathButton Width="24" Height="24"
Data="M50,50 L100,100 L150,50"
Stretch="Uniform" />
</Grid>
如果有人知道任何好的资源,甚至可以将一个快速的例子鞭在一起,我们将不胜感激。我认为创建按钮作为自定义控件非常简单。
通过自定义控制,我的意思是编译成我可以共享的DLL。
答案 0 :(得分:3)
代码背后:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:PathButton Data="M50,50 L100,100 L150,50" />
</Grid>
XAML:
{{1}}
答案 1 :(得分:1)
你去吧
public class LazyButton : Button
{
public static readonly DependencyProperty DataProperty = DependencyProperty.Register("Data", typeof(Geometry), typeof(LazyButton), new PropertyMetadata(new PropertyChangedCallback(OnDataChanged)));
private static void OnDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
LazyButton button = d as LazyButton;
button.Content = new Path() { Data = e.NewValue as Geometry, Fill = Brushes.Gray, Stretch = Stretch.Uniform };
}
public Geometry Data
{
get { return (Geometry)GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}
}
这在xaml中可用:
<local:LazyButton Data="M50,50 L100,100 L150,50"></local:LazyButton>
我确定你可以弄清楚如何在xaml中设置Fill和Stretch选项