Menü Schließen

WPF Fix: Style is Only Applied to First Element

There is a bug in WPF that leads to a very strange behavior:

A style that is defined in the resources is not applied to the first element, but to all others.

This only happens when MergedDictionaries are cascaded.

The following code is a valid XAML definition, but will not work as expected:

<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <!--Attach Default Fluent Control's Theme-->
      <ResourceDictionary Source="pack://application:,,,/Fluent;Component/Themes/Office2010/Silver.xaml"/>
      <ResourceDictionary>                
        <!-- DataGrid -->
        <Style x:Key="{x:Type DataGrid}" TargetType="{x:Type DataGrid}">
          <Setter Property="AlternatingRowBackground" Value="LightGray" />
        </Style>
      </ResourceDictionary>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>

This fixes the issue:

<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <!--Attach Default Fluent Control's Theme-->
      <ResourceDictionary Source="pack://application:,,,/Fluent;Component/Themes/Office2010/Silver.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <!-- DataGrid -->
    <Style x:Key="{x:Type DataGrid}" TargetType="{x:Type DataGrid}">
      <Setter Property="AlternatingRowBackground" Value="LightGray" />
    </Style>
  </ResourceDictionary>
</Application.Resources>

Only references to external dictionaries are nested within a merged dictionary. The rest is directly added to the resources.

Ähnliche Beiträge

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert