Changing the styles at runtime in WPF

前端 未结 3 861
走了就别回头了
走了就别回头了 2020-12-03 20:58

I am trying to allow the user to customize the elements in a WPF application. What I am trying to achieve is, if I have a list box which specifies all the form elements (Tex

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 21:28

    Have you tried using Resource Dictionaries

    Resource Dictionary

    
    
    
    

    XAML for the control

    
    

    Code to change styles at runtime

         var rd = new ResourceDictionary();
         rd.Add("TextColor", "#FFFFFF");
         Application.Current.Resources.MergedDictionaries.Add(rd);
    

    This will merge your new styles with the existing ones, and the change will be automatically reflected on all the controls linked with those styles.

提交回复
热议问题