Dynamically changing the control template in WPF

Source: Internet
Author: User
In some projects, you may need to dynamically change the template of the control. For example, you can select different themes in the software. The software interface and the style of the control vary under different themes, in this case, you can change the control template to implement the desired functions. The basic method is to use ResourceManager when you click the "Switch topic" button to load the new resource dictionary and use the newly loaded resource dictionary to replace the current resource dictionary. Assume that two different resource dictionary files dictionary1.xaml and dictionary2.xaml exist in the themes Folder: use one of the resource dictionaries in mainpage as the default style file:
  <Window.Resources>        <ResourceDictionary>            <ResourceDictionary.MergedDictionaries>                <ResourceDictionary Source="Themes\Dictionary1.xaml"></ResourceDictionary>            </ResourceDictionary.MergedDictionaries>        </ResourceDictionary>    </Window.Resources>
The following code switches the resource dictionary in the background:
    private void Button_Click_1(object sender, RoutedEventArgs e)        {            ResourceDictionary newDictionary = new ResourceDictionary();            newDictionary.Source = new Uri("Themes/Dictionary1.xaml", UriKind.RelativeOrAbsolute);            this.Resources.MergedDictionaries[0] = newDictionary;        }        private void Button_Click_2(object sender, RoutedEventArgs e)        {            ResourceDictionary newDictionary = new ResourceDictionary();            newDictionary.Source = new Uri("Themes/Dictionary2.xaml", UriKind.RelativeOrAbsolute);            this.Resources.MergedDictionaries[0] = newDictionary;        }

If you do not want the control to apply the default style, you can manually set the control style to NULL: <button content = "Topic 1" style = "{X: null} "horizontalalignment =" Left "verticalalignment =" TOP "width =" 75 "/> another method: directly modify the style and template of the control used, however, make sure that the style or template of the control is referenced as dynamicresource rather than staticresource.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.