Combobox background not being applied in windows 8

前端 未结 5 1142
迷失自我
迷失自我 2020-12-11 04:06

I am a little confused with wpf themes. I would like to have the wpf screens look the same on Vista, Windows 7 and Windows 8. So I have styled the components accordingly and

5条回答
  •  时光取名叫无心
    2020-12-11 05:00

    I did a bit of a hack on the built-in template. Not the cleanest solution, but removes the headache of having to roll my own template. The code behind basically binds the built-in template border's properties with the combo box's properties.

    
    
    
        private void ComboBox_Loaded(object sender, RoutedEventArgs e)
        {
            var comboBox = sender as ComboBox;
            var toggleButton = comboBox.Template?.FindName("toggleButton", comboBox) as ToggleButton;
            var border = toggleButton?.Template.FindName("templateRoot", toggleButton) as Border;
            if (border != null)
            {
                Binding b = new Binding("Background");
                b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(ComboBox), 1);
                BindingOperations.SetBinding(border, Control.BackgroundProperty, b);
    
                b = new Binding("BorderBrush");
                b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(ComboBox), 1);
                BindingOperations.SetBinding(border, Control.BorderBrushProperty, b);
            }
        }
    

提交回复
热议问题