【问题标题】:ControlTemplate wpf控制模板 wpf
【发布时间】:2017-10-12 13:58:16
【问题描述】:

如何将 多边形添加到 ControlTemplateControlTemplate to button 我怎么能做到这一点,但在代码中:

<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>

【问题讨论】:

标签: wpf controltemplate


【解决方案1】:

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

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;

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多