注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 CCNA教材推荐
 帮助

三十七:采用用字符串构造DataTemplate实现CheckListBox的CustomControl


2008-01-30 09:26:24
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://eallies.blog.51cto.com/375118/78992
为了方便大家学习,请单击此处下载该程序的代码。
  这次Themes\CheckListBox.xaml的内容相当简单:
    1 <ResourceDictionary
    2    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4    xmlns:local="clr-namespace:Eallies.OA.UI.Controls.Common">
    5 
    6     <Style TargetType="{x:Type local:CheckListBox}" BasedOn="{StaticResource {x:Type ListBox}}" />
    7 
    8 </ResourceDictionary>
  其具体的ItemTemplate,我们可以在OnInitialized方法中实现:
    1     protected override void OnInitialized(EventArgs e)
    2     {
    3         base.OnInitialized(e);
    4 
    5         string template = string.Format("<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><CheckBox Name=\"chkCheckBox\" Tag=\"{{Binding Path={0}}}\" Content=\"{{Binding Path={1}}}\" /></DataTemplate>", this.ItemValuePath, this.ItemContentPath);
    6         this.ItemTemplate = XamlReader.Load(XmlReader.Create(new StringReader(template))) as DataTemplate;
    7     }
  其它代码就很以前的方案一样了。由于这里没有Converter,所以代码会简单很多。

本文出自 “Denny_Zhang” 博客,请务必保留此出处http://eallies.blog.51cto.com/375118/78992


附件下载:
  程序代码




    文章评论
 
 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: