Changing WPF Listbox SelectedItem text color and highlight/background Color using C#

前端 未结 3 866
天命终不由人
天命终不由人 2020-12-10 18:05

I am trying to change the highlighted(selected) color and the highlighted text color of a wpf listbox at runtime. I have tried creating a style and applying it as follows:

3条回答
  •  一个人的身影
    2020-12-10 19:07

    Thanks to Vinkal and failedprogramming, I got everything working beautifully. I created the following Resources:

    
            
            
            
        
    

    And then applied the style to my listbox with:

    ItemContainerStyle="{DynamicResource ResourceKey=_ListBoxItemStyle}"
    

    And finally, change the solidcolorbrush resources (therefore changing the setter values) in my C# code by doing the following:

        this.Resources["ListSelectedColor"] = EmulatorPage.ListSelectedColor;
        this.Resources["ListTextSelectedColor"] = EmulatorPage.ListSelectedTextColor;
    

    Thank you to both of you!

提交回复
热议问题