读书人

dp有关问题期待解答

发布时间: 2012-02-19 19:43:39 作者: rapoo

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); }        } 

读书人网 >CAD教程

热点推荐