gpt4 book ai didi

c# - DepencyProperty 未从 UserControl 上的绑定(bind)更新

转载 作者:太空宇宙 更新时间:2023-11-03 11:00:03 25 4
gpt4 key购买 nike

因此,我正在尝试创建一个具有某些 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" };

在 View 上:

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

用户控制 View :

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

标题出现了,但项目没有出现。并不是说我添加了一些控制台打印来检查是否正在调用这些方法,但没有任何显示,即使是标题。我将 UserControl 的 DataContext 设置为其自身。

有什么想法吗?

编辑:

在@Garry Vass 的建议下,我添加了一个回调函数。新代码是:

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

有了这个,我可以看到 Header 的值发生变化,但是 Items 没有回调发生。

编辑:

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

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

public ViewModel()
{
TestList = new BindingList<object> { "Hello", "This", "is", "a", "Test" };
}

编辑:测试更多我发现错误来自于 DP ont eh usercontrol 绑定(bind)到 View 上的 DP,它绑定(bind)到 VM。

编辑:终于让它工作了。更深入地搜索,我发现了这个 link在 codeproject 上完美地解释了如何创建用户控件。

最佳答案

虽然看起来有悖常理,但 WPF 绑定(bind)引擎基本上会忽略代码中的 setter 和 getter,并使用其自己的版本,该版本位于 WPF 管道的深处。因此,您放在那里进行干预或在您的情况下进行检查的任何代码都不会被执行。

那么为什么他们在那里呢?它们是编译时的 helper ,还为您的 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

这声明了一个带有 Property Changed 回调的依赖属性。只要属性被设置,WPF 就会调用它。

通过将调试器锚定在控制台语句所在的位置,您将看到回调方法中发生的变化。

关于c# - DepencyProperty 未从 UserControl 上的绑定(bind)更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18021564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com