专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Silverlight

dp有关问题期待解答

发布时间:2011-06-27 20:14:41 文章来源:www.iduyao.cn 采编人员:星星草
dp问题期待解答
不多说,直接贴代码
在generic.xaml有如下代码
  <Style TargetType="datavis:LineItem">
  <Setter Property="YG" Value="600" />
  <Setter Property="Template">
  <Setter.Value>
  <ControlTemplate TargetType="datavis:LineItem">
  <Line X1="100" X2="200" Y1="{TemplateBinding YG}" Y2="{TemplateBinding YG}" Stroke="Black"/>
  </ControlTemplate>
  </Setter.Value>
  </Setter>
  </Style>
在LineItem类
 public class LineItem : Control
  {
  public LineItem()
  {
  this.DefaultStyleKey = typeof(LineItem);
  }
  public static readonly DependencyProperty YGProperty =
  DependencyProperty.Register(
  "YG",
  typeof(int),
  typeof(LineItem),
  null);
  public int YG
  {
  get { return (int)GetValue(LineItem.YGProperty); }
  set { SetValue(LineItem.YGProperty, value); }
  }
  }
现在的问题是我在使用的时候在xaml中设置YG值时,始终画在顶部,应该是没有设置成功,始终未0

------解决方案--------------------
试了一下,果然是一个隐藏很深,最后却让人很意外的问题。
首先,你尝试一下这样的Binding
<Line X1="0" X2="100" Y1="0" Y2="{Binding Path=YG, RelativeSource={RelativeSource TemplatedParent}}" Stroke="Black"/>
可以成功吧。

为什么这样呢?
问题没有出在Binding上
而是出
C# code

        public static readonly DependencyProperty YGProperty =
        DependencyProperty.Register(
        "YG",
        typeof(double),
        typeof(LineItem), null);

        public double YG
        {
            get { return (double)GetValue(YGProperty); }
            set { SetValue(YGProperty, value); }
        }
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: