datatemplate中的依赖属性未更新

时间:2014-12-29 14:59:30

标签: wpf datatemplate

我们有一个带有两个依赖属性Round和Depth的自定义控件。问题是当我在datatemplate中使用该控件时:

<DataTemplate x:Key="SyntDom" DataType="{x:Type entities:SwapInstrument}" >
...
    <depthOfMarket:DOMControl Grid.Row="1" Instrument="{Binding}" Margin="10,5" Round="False" Depth="38">
...
</DataTemplate>
....
<ContentControl ContentTemplate="{StaticResource SyntDom}" Content="{Binding Instruments.Instrument1}"/>

然后Round属性永远不会更新为False值,而Depth获取其38。 当我直接使用DOMControl而不用ContentControl包装它时它可以正常工作。

控制中这些属性的代码几乎是复制粘贴,所以我怀疑问题是否存在。

upd:依赖属性定义

public static readonly DependencyProperty DepthProperty = DependencyProperty.Register("Depth", typeof (int), typeof (DOMControl), new PropertyMetadata(OnChangedDepth));
public int Depth
{
    get { return (int) GetValue(DepthProperty); }
    set { SetValue(DepthProperty, value); }
}

private static void OnChangedDepth(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
    ((DOMControl) obj).OnChangedDepth();
}

private void OnChangedDepth()
{
    _dom.Depth = Depth;
    BindGrid();
}

public static readonly DependencyProperty RoundProperty = DependencyProperty.Register("Round", typeof (bool), typeof (DOMControl), new PropertyMetadata(OnChangedRound));

public bool Round
{
    get { return (bool) GetValue(RoundProperty); }
    set { SetValue(RoundProperty, value); }
}

private static void OnChangedRound(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
    ((DOMControl) obj).OnChangedRound();
}

private void OnChangedRound()
{
    _converterVolume.Round = Round;
}

1 个答案:

答案 0 :(得分:0)

所以我将Round属性定义为

public static readonly DependencyProperty RoundProperty = 
    DependencyProperty.Register("Round", typeof (bool), typeof (DOMControl), 
          new PropertyMetadata(OnChangedRound));

我已将其更改为

public static readonly DependencyProperty RoundProperty = 
    DependencyProperty.Register("Round", typeof (bool), typeof (DOMControl), 
          new PropertyMetadata(true, OnChangedRound));

即。为属性定义了一个默认的valyue,它现在可以正常工作了。

相关问题