简体   繁体   中英

WPF: Switching window styles

I have a program that has some buttons, one of them is used to switch "Themes". There are two themes, one is the normal Windows theme and the other is called Style2.

This is how I tried the switching

    private bool UsingWindowsStyle = true;
    private ResourceDictionary Style2= new ResourceDictionary() { Source = new Uri("/...;component/Resources/DefaultStyles.xaml", UriKind.RelativeOrAbsolute) };

    private void SwitchButton_Click(object sender, RoutedEventArgs e)
    {
        if (UsingWindowsStyle)
        {
            Resources.MergedDictionaries.Add(Style2);
            UsingWindowsStyle = false;
        }
        else
        {
            Resources.MergedDictionaries.Remove(Style2);
            UsingWindowsStyle = true;
        }
    }

My problem is, when I use this program, and press this Button , this is what happens:

Window Opened Program operating normally with Windows theme.

SwitchButton First Click Program changes visuals to the Style2 theme. All the program's buttons operating normally.

SwitchButton Second Click Program reverts back to Windows theme, but all the buttons in the program seize to work.

Points to Consider

  1. The program does not throw any exceptions at this point.
  2. Debugging the code, it seems that after the second click, the program does not enter the SwitchButton_Click method.
  3. I tried readding the Click EventHandler but with no use.

     SwitchButton.Click += new RoutedEventHandler(SwitchButton_Click); 

Thanks in advance for your help.

I would suggest that you are trying too hard. All you need to do is to change the Style on the Window itself. Leave the dictionaries alone. :-)

Here is an example that changes a windows style when you click from the list of available styles.

如何造型

My command boils down to

             //Here I am changing the style on the window
            NewWindow.Style = ((StyleDetailsViewModel)x).Style;
            NewWindow.Show();

with various input data

   public StylingViewModel(Func<string, Style> findStyle)
    {     
        Styles = new StyleDetailsViewModel[]
        {
            new StyleDetailsViewModel
            { 
                Name = "None",  
                Description = "Completely remove all styling and show the raw NavigationWindow including default navigation elements", 
                WindowStyleNone = false,
                Image = "\\Resources\\WindowStyleNone.png"
            },
            new StyleDetailsViewModel
            { 
                Name = "PlainWindow", 
                Style = findStyle("PlainWindow"),  
                Description = "Hides the navigation elemetns of the NavigationWindow to make it look just like a normal window", 
                WindowStyleNone = false,
                Image = "\\Resources\\WindowStylePlain.png"
            },
            new StyleDetailsViewModel
            { 
                Name = "Windows 7",  
                Style = findStyle("Win7NavigationWindow"),  
                Description = "Uses glass effects to create a window that looks almost identical to the control panel from Windows 7.", 
                WindowStyleNone = false,
                Image = "\\Resources\\WindowStyleWin7Nav.png"
            },

and

    this.DataContext = new StylingViewModel(x => (Style)this.FindResource(x));

Also beware of certain Window properties that can only be set before the window opens, such as WindowStyle="None" which you need if you are doing custom chrome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM