WPF自定义控件,DependencyProperty问题

时间:2011-07-10 04:49:59

标签: c# wpf .net-4.0 dependency-properties

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

/// <summary>
    /// Interaction logic for UCTest.xaml
    /// </summary>
    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代码没什么特别之处:

<UserControl ...>
    <Grid DataContext="{Binding}">

    </Grid>
</UserControl>

我的视图中的代码 - 模型:

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代码:

<Window...>
    <Grid>
        <CustomControlTest:UCTest LastName="{Binding LastName}"/>
    </Grid>
</Window>

我已设置断点并设置为“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,这是我的p revious post的后续问题。思想ID开始一个新的,因为真正的问题与我的原始帖子无关。 :)

1 个答案:

答案 0 :(得分:2)

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

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

this.Dispatcher.BeginInvoke((Action)(() =>
{
    viewModel.LastName = "asdf";

    viewModel.LastName = "56udfh";

    viewModel.LastName = "09ualkja";
}),
DispatcherPriority.DataBind);