WPF 获取ControlTemplate 中的控件方法

 
  
1 CheckBox cbSelect = (CheckBox)(ListBoxView.Items[i] as ListBoxItem).Template.FindName( " cbSelect " , ListBoxView.Items[i] as ListBoxItem);
2 TextBlock textblackConnPointNum = (TextBlock)(ListBoxView.Items[i] as ListBoxItem).Template.FindName( " textblackConnPointNum " , ListBoxView.Items[i] as ListBoxItem);
3 TextBlock textblackConnPointNam = (TextBlock)(ListBoxView.Items[i] as ListBoxItem).Template.FindName( " textblackConnPointName " , ListBoxView.Items[i] as ListBoxItem);
4 ComboBox cbGroup = (ComboBox)(ListBoxView.Items[i] as ListBoxItem).Template.FindName( " cbGroup " , ListBoxView.Items[i] as ListBoxItem);
5 CheckBox cbPublic = (CheckBox)(ListBoxView.Items[i] as ListBoxItem).Template.FindName( " cbPublic " , ListBoxView.Items[i] as ListBoxItem);

对应着xaml:

 
  
1 < ContentControl >
2 < Grid >
3 < CheckBox Name ="cbSelect" Margin ="2,0,0,0" VerticalAlignment ="Center" IsChecked =" {Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}} " <ContentPresenter Margin ="2" />
4 5   < TextBlock Width ="80" Name ="textblackConnPointNum" TextAlignment ="Center" Text =" {Binding Path=ConnPointNum} " />
6     < TextBlock Width ="150" Name ="textblackConnPointName" Text =" {Binding Path=ConnPointName} " TextAlignment ="Center" />
7 ......
8 </ Grid >
9 </ ContentControl >

在ControlTemplate中找控件最重要的就是让控件初始化到visualtree上,所以如何没有找到ControlTemplate的控件就有可能是控件还未能加载到visualtree。

可以用一下两种方式强制刷新模板控件至visualtree.

第一种是UpdateLayout()  方法。

但是MSDN提示:如果 UI 中存在大量元素,则频繁调用 InvalidateArrange(尤其是频繁调用 UpdateLayout)将会对性能产生重大影响。除非代码中后续调用其他 API 绝对需要精确的布局状态,否则请避免调用此方法。

第二种方案:

  private delegate void NoArgDelegate();

        public static void Refresh(DependencyObject obj)
        {

            obj.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.ApplicationIdle, (NoArgDelegate)delegate { });

        }



转载于:https://www.cnblogs.com/midcn/archive/2011/05/24/2055276.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值