WPF 中的代码隐藏静态/动态属性和命名难题
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 XAML 和隐藏代码并不等效。在 XAML 中,您将按钮的
Content
属性设置为ContentControl
。在后面的代码中,您将Template
(或Content
)属性设置为ApplicationVector
资源。您需要将按钮的
Content
属性设置为ContentControl
的实例,其Template
属性设置为您的ApplicationVector
资源。Your XAML and code behind are not equivalent. In the XAML, you are setting the
Content
property of the button to be aContentControl
. In the code behind, you are setting theTemplate
(orContent
) property to be theApplicationVector
resource.You would need to set the
Content
property of the button to be an instance of aContentControl
whoseTemplate
property is set to be yourApplicationVector
resource.