自定义类型的DependencyProperty不会触发propertychangedchanged回调

时间:2014-04-14 09:04:41

标签: c# wpf user-controls dependency-properties

前提:我阅读了所有其他关于类似问题的线索,但没有一个解决了我的问题。

我有一个带有3 DP的UserControl(SummarySource):

public static DependencyProperty QueryProperty;
public static DependencyProperty MaxRowsPerPageProperty;
public static DependencyProperty OpcSessionProperty;

各自的公众吸气者和二传手:

[Category("Common")]
public String Query
{
    get { return (String)GetValue(QueryProperty); }
    set { SetValue(QueryProperty, value); }
}

[Category("Common")]
public UInt32 MaxRowsPerPage
{
    get { return (UInt32)GetValue(MaxRowsPerPageProperty); }
    set { SetValue(MaxRowsPerPageProperty, value); }
}

[Category("Common")]
public UaSession OpcSession
{
    get { return (UaSession)GetValue(OpcSessionProperty); }
    set { SetValue(OpcSessionProperty, value); }
}

问题是,不会触发“OpcSession”变量(唯一一个自定义类型)的PropertyChanged Callback。

静态构造函数

OpcSessionProperty = DependencyProperty.Register("OpcSession", typeof(UaSession), typeof(SummarySource), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnSessionChanged)));

回调

private static void OnSessionChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    MessageBox.Show("He3e1");
    SummarySource thisControl = (SummarySource)sender;
    if (thisControl.DataContext != null)
    {
       ((DataRetriever)thisControl.DataContext).SetOpcSession((UaSession)e.NewValue);
    }
 }

永远不会显示MessageBox。如果我在其他回调上放置MessageBox.Show,我可以在加载使用该控件的表单或更改xaml中的值时看到该消息。

.Xaml

<Window.DataContext>
        <cs:UaSession x:Name="opcSession" EndpointUrl="opc.tcp://192.168.200.11:62543/Runtime"/>
</Window.DataContext>

<control:SummarySource x:Key="qq" MaxRowsPerPage="25" OpcSession="{Binding Path=DataContext, ElementName=window, PresentationTraceSources.TraceLevel=High, Mode=TwoWay}" />

输出中没有绑定错误

0 个答案:

没有答案
相关问题