zoukankan      html  css  js  c++  java
  • 基于继承类的属性模版中无法绑定的原因和解决方法

    这个的晚上想学学WPF 做一个类似于ERP 左边菜单。构思一下思路 。。。。。。

    创建一个类基于Expander类。

     1  public class ExpanderBox : Expander
     2     {
     3         #region 定义依赖属性
     4         
     5        
     6         //定义ItemsSource数据源
     7         public static readonly DependencyProperty ItemsSourcesProperty = DependencyProperty.Register("ItemsSources", typeof(object), typeof(ExpanderBox));
     8         //定义ItemsCommand ,Header头部点击命令。
     9         public static readonly DependencyProperty ItemsCommandProperty = DependencyProperty.Register("ItemsCommand", typeof(ICommand), typeof(ExpanderBox));
    10         //定义HeaderHeight,Header头部的高度。
    11         public static readonly DependencyProperty HeaderHeightProperty = DependencyProperty.Register("HeaderHeight", typeof(int), typeof(ExpanderBox));
    12         
    13         public object ItemsSources
    14         {
    15             get { return (object)GetValue(ExpanderBox.ItemsSourcesProperty); }
    16             set { SetValue(ExpanderBox.ItemsSourcesProperty, value); }
    17         }
    18         public ICommand ItemsCommand
    19         {
    20             get { return (ICommand)GetValue(ExpanderBox.ItemsCommandProperty); }
    21             set { SetValue(ExpanderBox.ItemsSourcesProperty, value); }
    22         }
    23 
    24         public int HeaderHeight
    25         {
    26             get { return (int)GetValue(ExpanderBox.HeaderHeightProperty); }
    27             set { SetValue(ExpanderBox.HeaderHeightProperty, value); }
    28         }
    29         #endregion
    30 
    31         //public  ExpanderBox()
    32         //{
    33         //    HeaderHeight = 10;
    34         //}
    35 
    36 
    37     }

    由于 Expander中Content中要放一个ListBox,所以定义了一个依赖属性ItemsSources,HeaderHeight是头部的高度。

    后然构造好了,就要写前台的样式了。

     1 <Style TargetType="{x:Type local:ExpanderBox}">
     2         <Setter Property="Template">
     3             <Setter.Value>
     4                 <ControlTemplate TargetType ="local:ExpanderBox">
     5                      <Grid>
     6                         <Grid.RowDefinitions>
     7                             <RowDefinition ></RowDefinition>
     8                             <RowDefinition></RowDefinition>
     9                         </Grid.RowDefinitions>
    10                         <ToggleButton Grid.Row="0" Height="{TemplateBinding HeaderHeight}" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Content="{TemplateBinding Header}" Command="{TemplateBinding ItemsCommand}"></ToggleButton>
    11                         <ListBox x:Name="itemsBox"  Grid.Row="1" ItemsSource="{TemplateBinding ItemsSources}" Visibility="Collapsed" ></ListBox>
    12                     </Grid>
    13                     <ControlTemplate.Triggers>
    14                         <Trigger Property="Expander.IsExpanded" Value="true">
    15                             <Setter Property="Visibility" Value="Visible" TargetName="itemsBox"></Setter>
    16                         </Trigger>
    17                     </ControlTemplate.Triggers>
    18                 </ControlTemplate>
    19             </Setter.Value>
    20         </Setter>
    21     </Style>

    从代码框中看到一条有背景色代码,为什么要标出来呢!请不要急,继续看下文

    在窗口中定义ExpanderBox

    1 <local:ExpanderBox x:Name="ex"   Header="采购合同" HeaderHeight="40"  ItemsSources="{Binding Lists}"></local:ExpanderBox>
    2             <local:ExpanderBox x:Name="ex1"  IsExpanded="True"  Header="采购合同" HeaderHeight="{Binding Height}"  ItemsSources="{Binding Lists}"></local:ExpanderBox>

    如我所料,运行结果如图!

    但是我发现,我无论怎样设置HeaderHeight的值,都无法改变ToggleButton高度。

    在网上找了一些资料,很幸运的是在微软网站中找到答案,我就不多写了,直接贴上原文

    网址:http://msdn.microsoft.com/zh-cn/library/ms742882.aspx

    对于模板方案来说,TemplateBinding 是绑定的优化形式,类似于使用 {Binding RelativeSource={RelativeSource TemplatedParent}} 构造的 Binding。 TemplateBinding 始终为单向绑定,即使所涉及的属性默认为双向绑定。 所涉及的两个属性都必须是依赖项属性。

    RelativeSource 是另一个标记扩展,有时与 TemplateBinding 结合使用或者代替它使用,以便在模板中执行相对属性绑定。

    此处未介绍控件模板概念;有关详细信息,请参阅 Control 样式和模板

    特性语法是最常用于该标记扩展的语法。 在 TemplateBinding 标识符字符串之后提供的字符串标记被指定为基础 TemplateBindingExtension 扩展类的 Property 值。

    对象元素语法也可行,但因为没有实际的应用,所以未进行演示。 TemplateBinding 用于使用计算的表达式来填充资源库内的值,因此使用 TemplateBinding 的对象元素语法来填充 <Setter.Property> 属性元素语法就会变得繁冗而多余。

    TemplateBinding 还可以在详细特性用法中使用,以便将 Property 属性指定为一个 property=value 对:

    看完这句话不能明白上面有两种背景颜色的代码。前面是单向绑定,所以在前台赋值无效,而后者是双向绑定。

    总结:

    TemplateBinding 是单向绑定形式。

    Binding是多向的。

     

    以上只是个人想法和实践经验,如果有文字错误和语法错误,请加以指点!

    QQ:247039968

    emil:wujc@younger.com

    无论是美女的歌声,还是鬣狗的狂吠,无论是鳄鱼的眼泪,还是恶狼的嚎叫,都不会使我动摇

  • 相关阅读:
    自我介绍
    oracle 几种分页
    for update nowait
    彻底卸载SQL 2005
    如何将 SQL SERVER 彻底卸载干净
    iBatis 到 MyBatis区别
    SqlMapConfig.xml配置文件详解
    mybatis简单应用(基于配置文件)
    ibatis学习笔记一:sqlMapConfig.xml文件配置详解
    Extjs 中的添加事件总结
  • 原文地址:https://www.cnblogs.com/qq247039968/p/4032327.html
Copyright © 2011-2022 走看看