The property 'Content' is set more than once when I add Style

时间:2015-07-28 16:20:54

标签: c# wpf

When I use this:

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

It runs just fine.

But when I add the Style tag:

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

It doesn't compile saying:

The property 'Content' is set more than once

1 个答案:

答案 0 :(得分:14)

Because you set the content property twice. Putting more elements inside an element is the same thing as setting the content property without some additional information

Whenever you are looking to set a property besides content from inside the element you need to wrap it in <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>

is what you want