项目开发中遇到一个要求,需要将ComboBox右侧中的小三角箭头给去掉,通过Blend工具“编辑ComboBox的模板副本”得知,这是一个名为"BtnArrow"的Path。但是在CS代码中,是无法引用到这个控件的。
解决办法:重新定义一个类,继承自ComboBox,然后重写OnApplyTemplate方法,代码如下
02 |
using System.Windows.Controls; |
03 |
using System.Windows.Shapes; |
05 |
namespace ContentTemplateTest |
07 |
public class YJMComboBox : ComboBox |
10 |
public bool IsShowDropDownArrow |
12 |
get { return (bool)GetValue(IsShowDropDownArrowProperty); } |
13 |
set { SetValue(IsShowDropDownArrowProperty, value); } |
17 |
public static readonly DependencyProperty IsShowDropDownArrowProperty = |
18 |
DependencyProperty.Register("IsShowDropDownArrow", typeof(bool), typeof(YJMComboBox), new PropertyMetadata(true, OnIsShowDropDownArrowChanged)); |
21 |
static void OnIsShowDropDownArrowChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) |
23 |
YJMComboBox _yjmCombobox = obj as YJMComboBox; |
24 |
if (_yjmCombobox != null) |
26 |
if ((bool)args.NewValue) |
28 |
_yjmCombobox._DropDownToggleButton.Visibility = Visibility.Visible; |
32 |
_yjmCombobox._DropDownToggleButton.Visibility = Visibility.Collapsed; |
37 |
private Path _DropDownToggleButton = new Path(); |
39 |
public override void OnApplyTemplate() |
41 |
_DropDownToggleButton = GetTemplateChild("BtnArrow") as Path; |
42 |
base.OnApplyTemplate(); |
我增加了一个BOOL型的依赖属性IsShowDropDownArrow,并在OnApplyTemplate方法重载时获取了BtnArrow的引用,然后在IsShowDropDownArrow属性变化时,修改了BtnArrow的可视性。
注:
11 |
protected DependencyObject GetTemplateChild(string childName); |
通过查看GetTemplateChild方法的定义得知,这是一个Protected方法,所以只能在子类中使用,这也就是为什么在常规Xaml.cs文件中无法获取ContentTemplate中命名控件的原因。
剩下的事情就简单了,来测试一把!
xaml文件如下:
06 |
xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="ContentTemplateTest.MainPage" |
08 |
xmlns:local="clr-namespace:ContentTemplateTest" |
09 |
d:DesignHeight="300" d:DesignWidth="400"> |
13 |
<Grid x:Name="LayoutRoot" Background="White"> |
14 |
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center"> |
15 |
<local:YJMComboBox HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" x:Name="cbo"> |
18 |
<Button Content="Test" HorizontalAlignment="Center" VerticalAlignment="Top" Click="Button_Click" Margin="10,0,0,0"></Button> |
Xaml.cs部分:
02 |
using System.Windows.Controls; |
04 |
namespace ContentTemplateTest |
06 |
public partial class MainPage : UserControl |
10 |
InitializeComponent(); |
13 |
private void Button_Click(object sender, RoutedEventArgs e) |
15 |
this.cbo.IsShowDropDownArrow = !this.cbo.IsShowDropDownArrow; |