0
votes

I have created custom window classes for dialogs like this:

public class DialogBase : Window
{
  // common stuff here
}

public class DialogOkCancel : DialogBase
{
        static DialogOkCancel()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DialogOkCancel), 
                 new FrameworkPropertyMetadata(typeof(DialogOkCancel)));
        }
        // specific stuff here
}

The styles looks like this, and contains dialog specific things such as title bar, OK/Cancel buttons etc.

<Style x:Key="DialogBaseStyle" TargetType="{x:Type base:DialogBase}" BasedOn="{StaticResource {x:Type Window}}">
  <Setter Property="ShowInTaskbar" Value="False"/>
  <!-- more stuff here -->
</Style>

<Style TargetType="{x:Type base:DialogOkCancel}" BasedOn="{StaticResource DialogBaseStyle}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type base:DialogOkCancel}">
                <!-- more stuff here -->
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

This works perfectly fine during runtime. Also, the dialogs displays fine in XAML Designer.

However, in XAML Designer, the DialogOkCancel style gets applied to other all other windows as well, such as the application main window (which simply derives from plain Window). The title bar, buttons and everything. I don't really get this, as the TargetType is specific. This does not occur at runtime.

What am I not seeing here ?

1
Could be a bug in XAML Designer...Babbillumpa
It happens to me all the time, my solution is that i just put key on the style and add the to specific window . Fore some weird reason XAML Designer get's confused a lot when u have 'BasedOn' property and it points on some of the base controls!J.Memisevic
@J.Memisevic I was hoping to avoid key based styling in order to avoid repetitiveness.. but yeah, I am considering that as a last resort.Oyvind

1 Answers

-2
votes

Correct way to apply a style both while in designer mode and during runtime is to have declared the style in App.xaml. Best practice would be to have a MergedDictionaries, containing a reference of your various style dictionaries, which will be in other files.

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!-- Converters -->
            <ResourceDictionary Source="Resources/Converters.xaml"/>
            <!-- Windows -->
            <ResourceDictionary Source="Resources/WindowStyles.xaml"/>
            // ... and all others //

WindowStyles.xaml can contain the exact styles you wrote above. They will be applied to all DialogOkCancel windows, both in designer and runtime.

Hope that helped.