C#学习教程:WPF自定义控件,DependencyProperty问题分享


WPF自定义控件,DependencyProperty问题

我已经使用自定义控件设置了测试代码:

///  /// Interaction logic for UCTest.xaml ///  public partial class UCTest : UserControl { public static readonly DependencyProperty LastNameProperty = DependencyProperty.Register("LastName", typeof(string), typeof(UCTest), new PropertyMetadata("No Name", LastNameChangedCallback, LastNameCoerceCallback), LastNameValidateCallback); private static void LastNameChangedCallback( DependencyObject obj, DependencyPropertyChangedEventArgs e) { Console.WriteLine(e.OldValue + " " + e.NewValue); } private static object LastNameCoerceCallback(DependencyObject obj, object o) { string s = o as string; if (s.Length > 8) s = s.Substring(0, 8); return s; } private static bool LastNameValidateCallback(object value) { return value != null; } public string LastName { get { return (string)GetValue(LastNameProperty); } set { SetValue(LastNameProperty, value); } } public UCTest() { InitializeComponent(); } } 

XAML代码没什么特别之处:

     

我的视图模型中的代码:

 public class ViewModel : DependencyObject { public static readonly DependencyProperty LastNameProperty = DependencyProperty.Register("LastName", typeof(string), typeof(ViewModel)); public string LastName { get { return (string)GetValue(LastNameProperty); } set { SetValue(LastNameProperty, value); } } } 

MainWondow.XAML.cs中的代码:

 public MainWindow() { InitializeComponent(); var viewModel = new ViewModel(); DataContext = viewModel; viewModel.LastName = "asdf"; viewModel.LastName = "56udfh"; viewModel.LastName = "09ualkja"; } 

MainWindow.XAML代码:

      

我已经设置了断点并设置为“When Hit”。 输出(如下所示)显示仅在最后一次执行“LastName”时调用的属性。 我希望它可以在每次执行时调用on更改…任何想法为什么?

 Step into: Stepping over non-user code 'CustomControlTest.App.App' Step into: Stepping over non-user code 'CustomControlTest.App.InitializeComponent' Function: CustomControlTest.UCTest.LastNameValidateCallback(object), Thread: 0x1DDC Main Thread Function: CustomControlTest.UCTest.LastNameValidateCallback(object), Thread: 0x1DDC Main Thread Function: CustomControlTest.UCTest.LastNameValidateCallback(object), Thread: 0x1DDC Main Thread Function: CustomControlTest.UCTest.LastNameCoerceCallback(System.Windows.DependencyObject, object), Thread: 0x1DDC Main Thread Function: CustomControlTest.UCTest.LastNameValidateCallback(object), Thread: 0x1DDC Main Thread Function: CustomControlTest.UCTest.LastNameValidateCallback(object), Thread: 0x1DDC Main Thread Function: CustomControlTest.UCTest.LastNameCoerceCallback(System.Windows.DependencyObject, object), Thread: 0x1DDC Main Thread Function: CustomControlTest.UCTest.LastNameChangedCallback(System.Windows.DependencyObject, System.Windows.DependencyPropertyChangedEventArgs), Thread: 0x1DDC Main Thread No Name 09ualkja 

谢谢,

PS,这是我上一篇文章的后续问题。 思想ID开始一个新的,因为真正的问题与我的原始post无关。 🙂

您的控件未加载到MainWindow构造函数中,因此尚未建立Binding。 您可以通过在最后一个赋值,更改处理程序和构造函数末尾设置断点来检查这一点。 你会看到构造函数完成,直到UCTest的属性发生变化。

您可以通过使用Dispatcher推迟分配来解决此问题:

上述就是C#学习教程:WPF自定义控件,DependencyProperty问题分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—猴子技术宅(www.ssfiction.com)

 this.Dispatcher.BeginInvoke((Action)(() => { viewModel.LastName = "asdf"; viewModel.LastName = "56udfh"; viewModel.LastName = "09ualkja"; }), DispatcherPriority.DataBind); 

本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ssfiction.com/ckf/1301120.html

(0)
上一篇 2023年1月8日 下午1:16
下一篇 2023年1月8日 下午5:38

精彩推荐