C# 未从UserControl上的绑定更新DependencyProperty

C# 未从UserControl上的绑定更新DependencyProperty,c#,wpf,data-binding,user-controls,dependency-properties,C#,Wpf,Data Binding,User Controls,Dependency Properties,因此,我试图创建一个具有一些DependencyProperties的UserControl,以便可以重用代码。但是,某些属性没有更新,而其他属性正在更新。更奇怪的是,即使对于那些正在更新的属性,“set”方法也根本没有被调用 这是我的密码: 在ViewModel上: public ICommand TestCommand = new DelegateCommand(x => MessageBox.Show("Ocorreu Evento")); public List<string

因此,我试图创建一个具有一些DependencyProperties的UserControl,以便可以重用代码。但是,某些属性没有更新,而其他属性正在更新。更奇怪的是,即使对于那些正在更新的属性,“set”方法也根本没有被调用

这是我的密码:

在ViewModel上:

public ICommand TestCommand = new DelegateCommand(x => MessageBox.Show("Ocorreu Evento"));
public List<string> TestList = new List<string> { "Hello","This","is","a","Test" };
public ICommand TestCommand=newdelegatecommand(x=>MessageBox.Show(“Ocorreu Evento”);
public List TestList=新列表{“Hello”、“This”、“is”、“a”、“Test”};
关于这一观点:

<views:CustomizedList Header="Testing"
                        Items="{Binding TestList}"/>

用户控件视图:

<StackPanel>
    <Label Content="{Binding Header}" />
    <ListBox ItemsSource="{Binding Items}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="Hello"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

用户控制代码:

    public string Header
    {
        get { return (string)GetValue(HeaderProperty); }
        set
        {
            MessageBox.Show("New header is " + value); 
            SetValue(HeaderProperty, value);
        }
    }       

    public BindingList<object> Items
    {
        get { return (BindingList<object>)GetValue(ItemsProperty); }
        set {
            SetValue(ItemsProperty, value);
            Console.WriteLine("Value was set with " + value.Count + " items.");
        }
    }

    public static readonly DependencyProperty HeaderProperty =
        DependencyProperty.Register("Header", typeof(string),
          typeof(ListaCustomizada));

    public static readonly DependencyProperty ItemsProperty =
        DependencyProperty.Register("Items", typeof(BindingList<object>),
          typeof(ListaCustomizada));
公共字符串头
{
get{return(string)GetValue(HeaderProperty);}
设置
{
MessageBox.Show(“新标题为”+值);
设置值(HeaderProperty,值);
}
}       
公共绑定列表项
{
get{return(BindingList)GetValue(ItemsProperty);}
设置{
设置值(项目属性、值);
WriteLine(“值设置为“+Value.Count+”items”);
}
}
公共静态只读从属属性HeaderProperty=
DependencyProperty.Register(“标题”,类型(字符串),
类型(ListaCustomizada));
公共静态只读从属属性ItemsProperty=
DependencyProperty.Register(“项”),类型(BindingList),
类型(ListaCustomizada));
标题显示,但项目未显示。并不是说我添加了一些控制台打印来检查是否调用了这些方法,而是没有显示任何内容,即使是标题。我将UserControl的DataContext设置为自身

有什么想法吗

编辑:

在@Garry Vass sugestion之后,我添加了一个回调函数。新守则是:

    public static readonly DependencyProperty HeaderProperty =
        DependencyProperty.Register("Header", typeof(string),
          typeof(ListaCustomizada), new PropertyMetadata("", ChangedCallback));

    public static readonly DependencyProperty ItemsProperty =
        DependencyProperty.Register("Items", typeof(BindingList<object>),
          typeof(ListaCustomizada), new PropertyMetadata(new BindingList<object>(), ChangedCallback));

    private static void ChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Console.WriteLine("Dependency property is now " + e.NewValue);
    }
公共静态只读从属属性HeaderProperty=
DependencyProperty.Register(“标题”,类型(字符串),
typeof(ListaCustomizada),新的PropertyMetadata(“,ChangedCallback”);
公共静态只读从属属性ItemsProperty=
DependencyProperty.Register(“项”),类型(BindingList),
typeof(ListaCustomizada),newpropertyMetadata(newbindingList(),ChangedCallback));
私有静态void ChangedCallback(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
WriteLine(“依赖项属性现在是”+e.NewValue);
}
有了这个,我可以看到Header的值发生了变化,但是没有对条目进行回调

编辑:

将TestList更改为属性而不是字段,将其类型更改为BindingList以与用户控件中的数据保持一致,结果仍然相同

    public BindingList<object> TestList { get; set; }

    public ViewModel()
    {
        TestList = new BindingList<object> { "Hello", "This", "is", "a", "Test" };
    }
publicbindinglist TestList{get;set;}
公共视图模型()
{
TestList=newbindingslist{“Hello”、“This”、“is”、“a”、“Test”};
}
编辑: 通过进一步测试,我发现错误来自这样一个事实,即DP ont eh usercontrol绑定到视图上的一个DP,该DP绑定到VM

编辑:
终于成功了。我在codeproject中更深入地搜索了一下,它完美地解释了如何创建用户控件

虽然WPF绑定引擎看起来有悖常理,但它基本上忽略了代码中的setter和getter,并使用其自己的版本,该版本位于WPF管道的深处。因此,任何你放在那里干预的代码,或者在你的情况下,检查的代码,都将处于未执行状态

那他们为什么会在那里?它们是编译时的助手,也为您的Xaml提供了一些连接的东西

如果您想干预或检查依赖项属性发生了什么,您可以这样声明它

    #region MyProperty (DependencyProperty)
    public int MyProperty
    {
        get { return (int)GetValue(MyPropertyProperty); }
        set { SetValue(MyPropertyProperty, value); }
    }
    public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.Register("MyProperty", typeof(int), typeof(MainWindow),
          new PropertyMetadata(0, ChangedCallback));
    private static void ChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Console.WriteLine("Dependency property is now " + e.NewValue);
    }
    #endregion
这声明了一个属性已更改的依赖属性回调。只要设置了属性,WPF就会调用它


通过将调试器锚定在Console语句所在的位置,您将看到回调方法中发生的更改。

WPF中的DependencyProperties
是一种特殊的属性。话虽如此,您的
Console.WriteLine()
MessageBox.Show()
很可能是原因
DependencyProperties
除了它们原来的getter和setter之外,不应该有任何额外的内容。好吧,我在注意到没有任何更改后添加了它们,所以它们不是根本原因。我删除了它们,得到了相同的症状:显示了标题值,但没有显示集合。从外观上看,应该是我尝试过的双绑定(ViewModel->View->UserControl)。它适用于标头(我可以看到值已更改),但我怀疑没有调用Items属性的回调。您的“TestList”必须是具有公共getter和setter的属性。您正在将其初始化为字段。创建一个属性并在VM的构造函数中初始化它。另外,人们喜欢使用ObservableCollection而不是List,但是如果您想绑定它,它仍然必须是一个属性。为TestList添加了一个{get;set;},将类型更改为BindingList,仍然得到相同的结果。我现在看到了。这与你的虚拟机有关!主窗口的DC设置在哪里?我相信VM也可以。我只是复制和粘贴重要的部分,但是在我将视图的datacontext(它是一个usercontrol)设置到VM之后不久。所有绑定都在VM/V之间工作,问题在于如何跳转VM->V->UC