Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自定义控件:继承的依赖项属性更改时如何调用方法?_C#_.net_Wpf_Xaml_Uwp - Fatal编程技术网

C# 自定义控件:继承的依赖项属性更改时如何调用方法?

C# 自定义控件:继承的依赖项属性更改时如何调用方法?,c#,.net,wpf,xaml,uwp,C#,.net,Wpf,Xaml,Uwp,我正在编写一个继承ItemsControl的自定义控件。每当某些属性发生更改时,我需要调用一个方法。对于我自己的依赖属性,我可以在setter中调用它,这没有问题,但是对于像ItemsSource这样的继承属性,我不知道如何做到这一点,我想学习如何在不覆盖整个内容的情况下做到这一点 在搜索时,我发现这至少可以通过WPF中的OverrideMetadata实现(我的项目是UWP)。我了解如何使用OverrideMetadata更改默认值,但是我不知道如何将其用作属性更改通知。对于ItemsSour

我正在编写一个继承ItemsControl的自定义控件。每当某些属性发生更改时,我需要调用一个方法。对于我自己的依赖属性,我可以在setter中调用它,这没有问题,但是对于像ItemsSource这样的继承属性,我不知道如何做到这一点,我想学习如何在不覆盖整个内容的情况下做到这一点


在搜索时,我发现这至少可以通过WPF中的OverrideMetadata实现(我的项目是UWP)。我了解如何使用OverrideMetadata更改默认值,但是我不知道如何将其用作属性更改通知。

对于
ItemsSource
属性,您可以只覆盖
OnItemsSourceChanged
方法,但是对于任何其他依赖性属性,您可以使用
DependencyPropertyDescriptor

public class MyItemsControl : ItemsControl
{
    public MyItemsControl()
    {
        DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor
            .FromProperty(ItemsControl.ItemsSourceProperty, typeof(ItemsControl));
        if (dpd != null)
        {
            dpd.AddValueChanged(this, OnMyItemsSourceChange);
        }

    }

    private void OnMyItemsSourceChange(object sender, EventArgs e)
    {
        //...
    }
}

这就是WPF。在UWP应用程序中,您应该能够使用@Thomas Levesque的
DependencyPropertyWatcher
类:

对于
ItemsSource
属性,您可以只覆盖
OnItemsSourceChanged
方法,但是对于任何其他依赖属性,您可以使用
DependencyPropertyDescriptor

public class MyItemsControl : ItemsControl
{
    public MyItemsControl()
    {
        DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor
            .FromProperty(ItemsControl.ItemsSourceProperty, typeof(ItemsControl));
        if (dpd != null)
        {
            dpd.AddValueChanged(this, OnMyItemsSourceChange);
        }

    }

    private void OnMyItemsSourceChange(object sender, EventArgs e)
    {
        //...
    }
}

这就是WPF。在UWP应用程序中,您应该能够使用@Thomas Levesque的
DependencyPropertyWatcher
类:

UWP中有一个新方法,名为
RegisterPropertyChangedCallback
,专门为此设计。例如,以下是如何在扩展的
GridView
控件中删除默认入口过渡

// Remove the default entrance transition if existed.
RegisterPropertyChangedCallback(ItemContainerTransitionsProperty, (s, e) =>
{
    var entranceThemeTransition = ItemContainerTransitions.OfType<EntranceThemeTransition>().SingleOrDefault();
    if (entranceThemeTransition != null)
    {
        ItemContainerTransitions.Remove(entranceThemeTransition);
    }
})
//删除默认入口转换(如果存在)。
RegisterPropertyChangedCallback(ItemContainerTransitionsProperty,(s,e)=>
{
var enterthemetransition=ItemContainerTransitions.OfType().SingleOrDefault();
if(enterthemetransition!=null)
{
ItemContainerTransitions.移除(进入Metransition);
}
})
您可以使用
取消注册PropertyChangedCallback
取消注册


可以找到更多信息。

在UWP中有一种新方法,名为
RegisterPropertyChangedCallback
,专门为此设计。例如,以下是如何在扩展的
GridView
控件中删除默认入口过渡

// Remove the default entrance transition if existed.
RegisterPropertyChangedCallback(ItemContainerTransitionsProperty, (s, e) =>
{
    var entranceThemeTransition = ItemContainerTransitions.OfType<EntranceThemeTransition>().SingleOrDefault();
    if (entranceThemeTransition != null)
    {
        ItemContainerTransitions.Remove(entranceThemeTransition);
    }
})
//删除默认入口转换(如果存在)。
RegisterPropertyChangedCallback(ItemContainerTransitionsProperty,(s,e)=>
{
var enterthemetransition=ItemContainerTransitions.OfType().SingleOrDefault();
if(enterthemetransition!=null)
{
ItemContainerTransitions.移除(进入Metransition);
}
})
您可以使用
取消注册PropertyChangedCallback
取消注册


可以找到更多信息。

谢谢,我不知道有关OnItemChanged的信息,这很好。如果需要的话,我会尝试一下Thomas Levesque类。我想原生方法比watcher的绑定方法更整洁(更高效?)。谢谢,我不知道OnItemChanged,这很好。如果需要的话,我会尝试一下Thomas Levesque类。我觉得原生方法比watcher的绑定方法更整洁(也更有效?)。我只是覆盖了在mm8指出之前我没有注意到的OnItrmsChanged,但这显然是任何其他属性更改的正确解决方案。我只是覆盖了在mm8指出之前我没有注意到的OnItrmsChanged,但这显然是任何其他财产变动的正确解决方案。有人能告诉我为什么这是一个坏问题,这样我就不会有更多的声誉损失吗?有人能告诉我为什么这是一个坏问题,这样我就不会有更多的声誉损失吗?