控制模板wpf
如何将多边形添加到ControlTemplate和控制模板wpf,wpf,controltemplate,Wpf,Controltemplate,如何将多边形添加到ControlTemplate和ControlTemplate添加到按钮 除了代码外,我如何执行此操作: <Button x:Name="btnNew" Content="click me" HorizontalAlignment="Left" Margin="54,10,0,0" VerticalAlignment="Top" Width="250" Height="250" BorderThickness="0" UseLayoutRounding="True" &g
ControlTemplate添加到按钮
除了代码外,我如何执行此操作:
<Button x:Name="btnNew" Content="click me" HorizontalAlignment="Left" Margin="54,10,0,0" VerticalAlignment="Top" Width="250" Height="250" BorderThickness="0" UseLayoutRounding="True" >
<Button.Template>
<ControlTemplate>
<Polygon x:Name="poly" Points="0,50 200,20 230,150 20,200">
<Polygon.Fill>
<ImageBrush x:Name="imgtem" Stretch="Fill" ImageSource="Images/67.png">
</ImageBrush>
</Polygon.Fill>
</Polygon>
</ControlTemplate>
</Button.Template>
</Button>
您可以使用
XamlReader.Parse
创建ControlTemplate
,然后只需设置按钮的属性即可:
string xaml = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"><Polygon x:Name=\"poly\" Points=\"0,50 200,20 230,150 20,200\"><Polygon.Fill><ImageBrush x:Name=\"imgtem\" Stretch=\"Fill\" ImageSource=\"Images/67.png\"></ImageBrush></Polygon.Fill></Polygon></ControlTemplate>";
ControlTemplate template = System.Windows.Markup.XamlReader.Parse(xaml) as ControlTemplate;
Button button = new Button();
button.Name = "btnNew";
button.Content = "Click me";
button.HorizontalAlignment = HorizontalAlignment.Left;
button.Margin = new Thickness(54, 10, 0, 0);
//...
button.Template = template;
string xaml=”“;
ControlTemplate=System.Windows.Markup.XamlReader.Parse(xaml)作为ControlTemplate;
按钮按钮=新按钮();
button.Name=“btnNew”;
button.Content=“单击我”;
button.HorizontalAlignment=HorizontalAlignment.Left;
边距=新厚度(54,10,0,0);
//...
button.Template=模板;