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

为了方便你们学习,请 单击此处下载该程序的代码。
  此次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,因此代码会简单不少。
相关文章
相关标签/搜索