How to set style to window with ResourceDictionary

I am creating a ResourceDictionary named Dictionary1.xaml

, here is the code:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type Window}">
    <Setter Property="Background" Value="Red">
    </Setter>
</Style>

      

I return it to App.xaml or MainWindow.xaml like this:

<Application.Resources>
    <ResourceDictionary Source="Dictionary1.xaml" />
</Application.Resources>

 <Window.Resources>
    <ResourceDictionary Source="Dictionary1.xaml" />
 </Window.Resources>

      

And in the View Designer, the background background changes to red, but when the app is running, its default background (white), why? How to do it?

+3


source to share


1 answer


you can style other controls this way, except for Window

! try to set sytle for Button, Lable, etc, you will get correct result. But for a window, you won't.

see my other answer, this might help you: How do I add a common control for all my Windows?

you have to set the style x:Key

and set the window style explicitly:

Style="{DynamicResource key_name}"



why is the View Designer showing the correct result? it might be a mistake. vs2012 / 13 xaml The constructor has a lot of errors, you can search or commit in msdn.Ive commit one in it:

https://connect.microsoft.com/VisualStudio/feedbackdetail/view/925324/multibinding-report-an-issue-on-latest-vs-xaml-editor

but ms closes it and they won't re-process it lately.

0


source







All Articles