WPF 中的代码隐藏静态/动态属性和命名难题

发布于 2024-12-19 18:39:02 字数 993 浏览 2 评论 0原文

我在 XAML 中定义了一个按钮,它具有按钮样式和矢量图形,它是这样定义的:

    <Button
        Height="48"
        Width="48"
        mvp:Presenter.Action="CreateStudentApplication"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Grid.Row="5"
        Grid.Column="3"
        Style="{DynamicResource BaseButton2}">
        <ContentControl Template="{StaticResource ApplicationVector}"/>
    </Button>

在我的代码隐藏中,我有一种方法可以动态地将与此类似的新按钮添加到 StackPanel 中。简而言之,它做了一些事情:

            var button = new Button();
            button.Height = 48;
            button.Width = 48;
            button.Tag = x.ID;
            button.SetResourceReference(TemplateProperty, "ApplicationVector");
            button.SetResourceReference(StyleProperty, "BaseButton2");

现在这是奇怪的部分 - 它只显示矢量图形,后面没有按钮。当我删除倒数第二行(带有矢量参考的行)时,它会显示应有样式的按钮!我假设设置模板会覆盖样式,但它们似乎在 XAML 中友好地运行。我还尝试将 ContentProperty 设置为 TemplateProperty,但这会产生该类型的字符串。有什么想法吗?谢谢你!

I have a button defined in my XAML which has a button style and a vector graphic, it is defined thus:

    <Button
        Height="48"
        Width="48"
        mvp:Presenter.Action="CreateStudentApplication"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Grid.Row="5"
        Grid.Column="3"
        Style="{DynamicResource BaseButton2}">
        <ContentControl Template="{StaticResource ApplicationVector}"/>
    </Button>

In my code-behind, I have a method that dynamically adds new buttons similar to this one to a StackPanel. In brief, it does something to the tune of:

            var button = new Button();
            button.Height = 48;
            button.Width = 48;
            button.Tag = x.ID;
            button.SetResourceReference(TemplateProperty, "ApplicationVector");
            button.SetResourceReference(StyleProperty, "BaseButton2");

Now here's the weird part- It displays only the vector graphic, and no button behind it. When I remove the penultimate line (the line with the vector reference), it displays the button styled as it should be! I'm assuming that setting the template is overriding the style, however they seem to play amicably in XAML. I have also tried setting the ContentProperty instidead of TemplateProperty, but this resulted in a string of the type. Any ideas? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹雨成花 2024-12-26 18:39:02

您的 XAML 和隐藏代码并不等效。在 XAML 中,您将按钮的 Content 属性设置为 ContentControl。在后面的代码中,您将 Template (或 Content)属性设置为 ApplicationVector 资源。

您需要将按钮的 Content 属性设置为 ContentControl 的实例,其 Template 属性设置为您的 ApplicationVector 资源。

var contentControl = new ContentControl();
contentControl.SetResourceReference(TemplateProperty, "ApplicationVector");
button.Content = contentControl;

Your XAML and code behind are not equivalent. In the XAML, you are setting the Content property of the button to be a ContentControl. In the code behind, you are setting the Template (or Content) property to be the ApplicationVector resource.

You would need to set the Content property of the button to be an instance of a ContentControl whose Template property is set to be your ApplicationVector resource.

var contentControl = new ContentControl();
contentControl.SetResourceReference(TemplateProperty, "ApplicationVector");
button.Content = contentControl;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文