开发者

Split WPF Style XAML Files

开发者 https://www.devze.com 2022-12-22 23:10 出处:网络
Most WPF styles I have seen are split up into one very long Theme.xaml file. I want to split mine up for readability so my Theme.xaml looks like this:

Most WPF styles I have seen are split up into one very long Theme.xaml file. I want to split mine up for readability so my Theme.xaml looks like this:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/PresentationFramework.Aero;v3.0.0.0;31bf3856ad364e35;component/themes/aero.normalcolor.xaml"/>
        <ResourceDictionary Source="Controls/Brushes.xaml"/>
        <ResourceDictionary Source="Controls/Buttons.xaml"/>
        ...
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

The problem is that this solution does n开发者_JAVA技巧ot work. I have a default button style which is BasedOn the default Aero style for a button:

<Style x:Key="{x:Type Button}" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
    <Setter Property="FontSize" Value="14"/>
    ...
</Style>

If I place all of this in one file it works but as soon as I split it up I get StackOverflow exceptions because it thinks it is BasedOn itself. Is there a way around this? How does WPF add resources when merging resource dictionaries?


If both styles are always going to be applied, why not change the key on the lesser style (the one that is overridden later)? You would then reference the later one by that key rather than {StaticResource {x:Type Button}}. This should resolve the issue with determining the base type. It would also resolve a potential issue with styles being applied in the wrong order (e.g. if you redefine a single property multiple times, the order in which those dictionaries are applied would result in different results).

As long as the final descendant style has the correct key, the summation should be applied correctly.


If you omit the BasedOn, what are the properties that don't have the values you expect? I've noticed you can safely omit the BasedOn when you want to override a few properties, and the default style's template will still get applied.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号