首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ControlTemplate wpf

ControlTemplate wpf
EN

Stack Overflow用户
提问于 2017-10-12 13:58:16
回答 1查看 93关注 0票数 0

如何将多边形添加到ControlTemplate、ControlTemplate to button中,但在代码中如何做到这一点:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-12 14:11:33

您可以使用XamlReader.Parse创建ControlTemplate,然后简单地设置Button的属性

代码语言:javascript
复制
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;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46711541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档