自定义WPF中组合框的默认OnMouseOver属性
我想自定义Combobox的默认“OnMouseOver”-“color”,以及Combobox中下拉列表的“Background”颜色..我们可以自定义Combobox的上述属性吗..如果可以,请帮助我。。 以下是组合框的Xaml代码:自定义WPF中组合框的默认OnMouseOver属性,wpf,coding-style,combobox,background,Wpf,Coding Style,Combobox,Background,我想自定义Combobox的默认“OnMouseOver”-“color”,以及Combobox中下拉列表的“Background”颜色..我们可以自定义Combobox的上述属性吗..如果可以,请帮助我。。 以下是组合框的Xaml代码: <ComboBox Name="CmbBox1" BorderBrush="Black" Margin="1,1,1,1" ItemsSource="{Binding}">
<ComboBox Name="CmbBox1" BorderBrush="Black" Margin="1,1,1,1"
ItemsSource="{Binding}">
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Background="{Binding Background}" Foreground="Black" FontSize="10" TextAlignment="Left"
FontWeight="Black" Text="{Binding}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
这里有一种方法可以实现我认为您所追求的目标。我对您的
组合框做了一些更改
在Xaml代码中添加了注释,因此它应该是不言自明的
编辑。这在Windows 7下不起作用,因为组合框模板中的按钮框。您可以重新设置整个内容的模板,也可以使用此解决方案,它使用一些代码隐藏
首先,添加对PresentationFramework.Aero的引用
然后订阅组合框的Loaded
事件,禁用ButtonChrome
,并像这样在事件处理程序中绑定主网格背景
private void CmbBox1_Loaded(object sender, RoutedEventArgs e)
{
ComboBox comboBox = sender as ComboBox;
ToggleButton toggleButton = GetVisualChild<ToggleButton>(comboBox);
ButtonChrome chrome = toggleButton.Template.FindName("Chrome", toggleButton) as ButtonChrome;
chrome.RenderMouseOver = false;
chrome.RenderPressed = false;
chrome.RenderDefaulted = false;
chrome.Background = Brushes.Transparent;
Grid MainGrid = comboBox.Template.FindName("MainGrid", comboBox) as Grid;
Binding backgroundBinding = new Binding("Background");
backgroundBinding.Source = comboBox;
MainGrid.SetBinding(Grid.BackgroundProperty, backgroundBinding);
}
private static T GetVisualChild<T>(DependencyObject parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
private void CmbBox1\u已加载(对象发送方,路由目标e)
{
ComboBox ComboBox=发件人作为ComboBox;
ToggleButton ToggleButton=GetVisualChild(组合框);
ButtonChrome chrome=toggleButton.Template.FindName(“chrome”,toggleButton)作为ButtonChrome;
chrome.RenderMouseOver=false;
chrome.RenderPressed=false;
chrome.RenderDefaulted=false;
chrome.Background=画笔。透明;
Grid MainGrid=comboBox.Template.FindName(“MainGrid”,comboBox)作为网格;
绑定背景绑定=新绑定(“背景”);
backgroundBinding.Source=comboBox;
MainGrid.SetBinding(Grid.BackgroundProperty,backgroundBinding);
}
私有静态T GetVisualChild(DependencyObject父对象),其中T:Visual
{
T child=默认值(T);
int numVisuals=VisualTreeHelper.GetChildrenCount(父级);
对于(int i=0;i
Xaml
谢谢..上面的代码可以更改组合框的下拉背景..但是,它无法更改组合框控件的默认鼠标悬停颜色..请告诉我是否有解决方法..@soma sekhar:这是什么意思?当您用鼠标悬停所选项目时,它会更改该项目的背景。是不是你在找别的东西?@soma sekhar:用一个完整的Xaml例子更新了我的答案。只要复制/粘贴它,它就会给你想要的行为。否则我就误解了这个问题。。我在XP上测试了这个。现在,当我在Windows7上试用时,我发现了问题所在。。问题是模板中的Chrome,我将更新我的answer@somasekhar:尝试更新的答案,看看该解决方案是否有效:)
<ComboBox Name="CmbBox1" BorderBrush="Black" Margin="1,1,1,1"
ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}"
Loaded="CmbBox1_Loaded"
Width="150">
<ComboBox.Resources>
<SolidColorBrush x:Key="MouseOverBrush"
Color="Red"/>
<SolidColorBrush x:Key="DropDownListBrush"
Color="Green"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent"/>
</ComboBox.Resources>
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBox}},
Path=IsDropDownOpen}"
Value="True">
<Setter Property="Background" Value="{StaticResource DropDownListBrush}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.ItemContainerStyle>
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource MouseOverBrush}"/>
</Trigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
<ComboBox.ItemTemplate>
<DataTemplate>
<Border x:Name="border" SnapsToDevicePixels="True">
<TextBlock Foreground="Black" FontSize="10" TextAlignment="Left"
FontWeight="Black" Text="{Binding}"></TextBlock>
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBoxItem}}, Path=IsMouseOver}" Value="True">
<Setter TargetName="border" Property="Background" Value="{StaticResource MouseOverBrush}"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>