添加Style时,属性'Content'被设置多次

use*_*741 8 c# wpf

我用这个时:

<Label Grid.Column="2"
       Grid.Row="8" 
       Content="{x:Static res:Strings.ToolPanelEditView_Validation_MandatoryField}" >
</Label>
Run Code Online (Sandbox Code Playgroud)

它运行得很好.

但是当我添加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>
Run Code Online (Sandbox Code Playgroud)

它没有编译说:

属性"内容"设置不止一次

pqu*_*est 14

因为您设置了两次内容属性.在元素中放置更多元素与设置content属性没有一些额外信息是一回事

每当您想要在元素内部设置属性时,您需要将其包装在内 <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>
Run Code Online (Sandbox Code Playgroud)

是你想要的