c# - 添加 Style 时多次设置属性 'Content'

标签 c# wpf

当我使用它时:

<Label Grid.Column="2"
       Grid.Row="8" 
       Content="{x:Static res:Strings.ToolPanelEditView_Validation_MandatoryField}" >
</Label>

它运行得很好。

但是当我添加 Style 标签时:

 <Label Grid.Column="2"
        Grid.Row="8" 
        Content="{x:Static res:Strings.ToolPanelEditView_Validation_MandatoryField}" >
    <Style>
        <Setter Property="Label.Margin" Value="0" />
    </Style>
</Label>

它不会编译说:

The property 'Content' is set more than once

最佳答案

因为您设置了两次内容属性。在一个元素中放置更多的元素与设置内容属性没有任何额外信息是一回事

无论什么时候你想要设置元素内部内容以外的属性,你都需要将它包装在 <Element.Property> 中。

<Label  Grid.Column="2"  Grid.Row="8" Content="{x:Static res:Strings.ToolPanelEditView_Validation_MandatoryField}" >
  <Label.Style>
    <Style>
        <Setter Property="Label.Margin" Value="0" />
    </Style>
  </Label.Style>
</Label>

是你想要的

关于c# - 添加 Style 时多次设置属性 'Content',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31681961/

相关文章:

c# - Linq to NHibernate 排序未按预期工作

c# - 程序不收集目录信息

c# 将 linq 数据库记录转换为类实例

c# - 系统线程定时器

WPf : Binding with more than one property at a time

c# - 仅垂直折叠文本框 - 如果它被隐藏,则获取实际高度

WPF MVVM 组合框颜色

用于创建可选属性的 C# 接口(interface)

c# - 如何测试我的 Linq IQueryable 是否已执行

c# - 如何正确绑定(bind) WPF TreeView 控件并有效地显示数据的层次结构?