我想自定义默认 - “OnMouseOver” - Combobox的“颜色”以及组合框中下拉列表的“背景”颜色..我们可以自定义上面提到的combobox属性..如果是的话,请帮帮我.. 以下是我的组合框的Xaml代码:
<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>
答案 0 :(得分:2)
这是一种做我认为你想做的事情的方法。我对您的ComboBox
进行了一些更改。
在Xaml代码中添加了注释,因此它应该是非常自我解释的
修改。由于ButtonChrome
ComboBox
内的Template
,这在Windows 7下无效。您可以重新模板化整个过程,也可以使用此变通方法,后者使用了一些代码。
首先,添加对 PresentationFramework.Aero 的引用
然后订阅Loaded
的{{1}}事件并禁用ComboBox
并在事件处理程序中绑定MainGrid背景,如下所示
ButtonChrome
的Xaml
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;
}