【发布时间】:2013-03-08 15:57:33
【问题描述】:
我正在尝试更改 ComboBox 的边框 - 一项看似简单的任务,但没有发生任何事情。
编辑:
这是我的代码:
以下内容出现在事件中:
cmb.BorderThickness = new Thickness(3);
cmb.BorderBrush = Brushes.Green;
这是 XAML:
<UserControl x:Class="QueryBuilder.FilterWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" MaxHeight="200" Background="White" PreviewMouseLeftButtonDown="UserControl_PreviewMouseLeftButtonDown">
<UserControl.Resources>
<Style TargetType="TextBox">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="BorderBrush" Value="Silver"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBoxBase}">
<Border Name="Border" Padding="1" Background="#FFFFFF" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" >
<ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border" Property="Background" Value="#EEEEEE"/>
<Setter TargetName="Border" Property="BorderBrush" Value="#EEEEEE"/>
<Setter Property="Foreground" Value="#888888"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="TreeViewItem">
<Setter Property="Template" Value="{StaticResource TreeViewWithLines}"/>
<Setter Property="Padding" Value="0,5,0,5"/>
<Setter Property="IsExpanded" Value="true"/>
</Style>
</UserControl.Resources>
<ScrollViewer ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto">
<TreeView Name="tvFilters" Margin="0,10,0,10" Background="White" BorderBrush="White"/>
</ScrollViewer>
</UserControl>
我使用 ComboBox 的默认控件模板,所以我不知道为什么它不起作用。有什么想法吗?
【问题讨论】:
-
不要在代码中操作 UI 元素。
-
你设置
BorderThickness了吗? -
您使用的是 Windows 8 吗?我似乎记得在 Windows 8 上呈现其他控件(例如
ToggleButton)的方式存在一些错误。我刚刚尝试了 Dom 在我的 Windows 8 机器上发布的解决方案,而BorderThickness更改了 @987654326 @ 没有。 -
@Benjamin - 我实际上使用的是 Windows 8。你会推荐什么?我已经让它适用于文本框,但不适用于组合框
-
kmatyaszek 答案可能是此时您唯一可行的选择。定义一个新的控件模板。
标签: c# .net wpf user-interface mvvm