惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 牟向阳

Implementing SQL Server Row and Cell Level Security Daily English 你知道你的後照鏡調錯了嗎?原來裡面隱藏著視線死角! Silverlight Freezing & Crash Silverlight 中实现Service同步调用 silverlight 4常用的多线程技术 推荐几款Silverlight Tools【转载】 一个配置文件的Mapping Emit Vs CodeDom Custom DataContractSerializerOperationBehavior SQL Service查询分析 WPF&Silverlight精髓 支持定位当前页,自定义排序的分页SQL(拒绝动态SQL) WCF学习经验分享,如何更好地学习WCF? 后台管理界面收集 两个使用的Ajax Demo 自学面向对象 发几个有价值的.net源码 我是如何带领团队开发项目的
Silverlight:获取ContentTemplate中的命名控件
牟向阳 · 2011-09-07 · via 博客园 - 牟向阳

项目开发中遇到一个要求,需要将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的可视性。

注:

03 //     在实例化的 System.Windows.Controls.ControlTemplate 可视化树中检索已命名的元素。
10 //     模板中的命名元素(如果已找到)。如果在模板中找不到具有名称 childName 的元素,则可能返回 null。
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">
17             </local:YJMComboBox>
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
08         public MainPage()
10             InitializeComponent();           
13         private void Button_Click(object sender, RoutedEventArgs e)
15             this.cbo.IsShowDropDownArrow = !this.cbo.IsShowDropDownArrow;