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

推荐订阅源

WordPress大学
WordPress大学
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
月光博客
月光博客
V
Visual Studio Blog
T
Tailwind CSS Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
Jina AI
Jina AI
J
Java Code Geeks
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
Recorded Future
Recorded Future
C
Check Point Blog
腾讯CDC
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
Hacker News: Ask HN
Hacker News: Ask HN
SecWiki News
SecWiki News
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cloudbric
Cloudbric
F
Full Disclosure
The Cloudflare Blog
Y
Y Combinator Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
S
Schneier on Security
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
AI
AI
N
News and Events Feed by Topic
T
Tor Project blog
P
Palo Alto Networks Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog

博客园 - 牟向阳

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;