Domanda

I have this resource dictionary:

<Application.Resources>        
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary x:Name="defaultStyles" Source="/ReuxablesLegacy;component/edge.xaml" />
            </ResourceDictionary.MergedDictionaries>

        </ResourceDictionary>
    </Application.Resources>

When I define the styles below, they overwrite the skin in the resource dictionary and i don't want that, I want to "merge" or "inherit" from the above:

 <Grid.Resources>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Margin" Value="0,0,0,10" />
            <Setter Property="Width" Value="200" />
        </Style>
        <Style TargetType="{x:Type ComboBox}">
            <Setter Property="Margin" Value="0,0,0,10" />
            <Setter Property="Width" Value="200" />
        </Style>
        <Style TargetType="{x:Type  DatePicker}" >
            <Setter Property="Margin" Value="0,0,0,10" />
            <Setter Property="Width" Value="200" />
        </Style>
    </Grid.Resources>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
</Grid>

How can i perhaps use the BasedOn property or something to achieve this?

È stato utile?

Soluzione

If your default style resource dictionary has entries like this...

<!--A Style that affects all TextBlocks-->
<Style TargetType="TextBlock">
  <Setter Property="HorizontalAlignment" Value="Center" />
  <Setter Property="FontFamily" Value="Comic Sans MS"/>
  <Setter Property="FontSize" Value="14"/>
</Style>

...then you can implement something like this...

<!--A Style that extends the previous TextBlock Style-->
<!--This is a "named style" with an x:Key of TitleText-->
<Style BasedOn="{StaticResource {x:Type TextBlock}}"
       TargetType="TextBlock"
       x:Key="TitleText">
  <Setter Property="FontSize" Value="26"/>
  <Setter Property="Foreground">
  <Setter.Value>
      <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
        <LinearGradientBrush.GradientStops>
          <GradientStop Offset="0.0" Color="#90DDDD" />
          <GradientStop Offset="1.0" Color="#5BFFFF" />
        </LinearGradientBrush.GradientStops>
      </LinearGradientBrush>
    </Setter.Value>
  </Setter>
</Style>

This approach will give the inheritance effect that you are after.

The MSDN reference is at http://msdn.microsoft.com/en-us/library/system.windows.style.basedon(v=vs.110).aspx

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top