ele*_*eep 3 .net c# wpf combobox wpf-controls
我正在开发一个WPF用户界面,我的窗口上有一个组合框.所以我希望用户能够从这个组合框中选择一个项目但是当它被选中时我不希望它以默认的蓝色突出显示.
我假设有一些方法可以在XAML中阻止它,但到目前为止我还没有找到解决方案.
谢谢.
PS我无法访问Expression Blend,所以如果有人建议解决方案可以在XAML中
编辑:只是为了让我更清楚我选择了我的意思是一旦你选择了一个值并且SelectionChanged事件被触发并且项目显示在组合框中,组合框被突出显示如下:
您需要通过样式设置选择的外观.
<Window.Resources>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Border Background="{TemplateBinding Background}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Border Margin="2" Grid.Row="0" Background="Azure" />
<ContentPresenter />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
此样式将自动应用于窗口中的任何组合框:
<StackPanel>
<ComboBox>
<ComboBoxItem>111</ComboBoxItem>
<ComboBoxItem>222</ComboBoxItem>
<ComboBoxItem>333</ComboBoxItem>
<ComboBoxItem>444</ComboBoxItem>
<ComboBoxItem>555</ComboBoxItem>
</ComboBox>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
你会看到如下:
UPD:为了从所选项目中删除突出显示,您需要修改实际用于这些目的的系统画笔.只需添加两个额外的样式:
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6726 次 |
最近记录: |