【问题标题】:WPF Custom Control Dependency Property setter not getting called?WPF自定义控件依赖属性设置器没有被调用?
【发布时间】:2014-09-03 16:57:34
【问题描述】:

我创建了一个继承自 TextBox 类的自定义控件 CustomTextBox。我创建了一个名为 CustomTextProperty 的依赖属性。

我已将此 DP 与我的 Viewmodel 属性绑定。

在注册 DP 时,我已经给出了属性更改回调,但它只会在我的控件最初在我的 xaml 加载时获取绑定数据时被调用一次。

当我尝试从视图中设置我的控件时,绑定的 VM 属性设置器不会被调用,并且 propertychangecallback 也不会被触发。

请帮忙!!

代码片段如下:

我的自定义控件

class CustomTextBox : TextBox
{
  public static readonly DependencyProperty CustomTextProperty = DependencyProperty.Register("CustomText",
                                                               typeof(string), typeof(CustomTextBox),
                                                               new FrameworkPropertyMetadata("CustomTextBox",
                                                               FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                                                               new PropertyChangedCallback(OnCustomPropertyChange)));

 public string CustomText
 {
   get { return (string)GetValue(CustomTextProperty); }
   set { SetValue(CustomTextProperty, value); }
 }

 private static void OnCustomPropertyChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
 {
   // This is Demo Application.
   // Code to be done Later...
 }
}

我的视图模型:

public class ViewModel : INotifyPropertyChanged
{
 private string textForTextBox;

 public string TextForCustomTextBox
 {
   get
   {
     return this.textForTextBox;
   }
   set
   {
     this.textForTextBox = value;

     this.OnPropertyChange("TextForCustomTextBox");
   }
 }

 public event PropertyChangedEventHandler PropertyChanged;

 public void OnPropertyChange(string name)
 {
   PropertyChangedEventHandler handler = PropertyChanged;

   if (handler != null)
   {
     handler(this, new PropertyChangedEventArgs(name));
   }
 }
}

我的带绑定的 Xaml 代码:

<custom:CustomTextBox x:Name="CustomTextBox" 
                                  CustomText="{Binding TextForCustomTextBox, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                  Grid.Row="1" HorizontalAlignment="Center" Width="200" Height="50" />

我的代码在后面设置 DataContext:

// My View Constructor
public View1()
{
  InitializeComponent();

  this.DataContext = new ViewModel();
}

【问题讨论】:

  • 邮政编码,你是如何从后面的代码中设置它的?
  • 设置DataContext 的代码在哪里?您是否在 XAML 或代码隐藏中的某处设置了 DataContext?您发布的所有内容看起来都可以正常工作。
  • 感谢您的回复...我已经编辑了上面的代码,显示要设置为我的 ViewModel 类实例的数据上下文。

标签: wpf dependency-properties


【解决方案1】:

您说您声明了一个CustomText DependencyProperty 并将数据绑定到您的视图模型TextForCustomTextBox 属性,这是正确的。然而,当你说你试图从视图中设置你的属性时,你就错了。

实际上所做的是从视图中设置CustomTextBox .Text 属性,而这与您的CustomTextBox.CustomText 属性无关。您可以像这样连接它们,尽管我不太确定这样做的意义何在:

<Views:CustomTextBox x:Name="CustomTextBox" Text="{Binding CustomText, RelativeSource={
    RelativeSource Self}, UpdateSourceTrigger=PropertyChanged}" CustomText="{Binding 
    TextForCustomTextBox, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
    Grid.Row="1" HorizontalAlignment="Center" Width="200" Height="50" />

【讨论】:

  • 感谢 Sheridan... 您提供的解决方案使其工作。实际上这样做没有任何意义,但我正在学习 WPF 并希望了解这项技术的所有内容。你能推荐我学习 WPF 的任何好的文档或手册吗?
【解决方案2】:

尝试在实际初始化之前设置您的 DataContext,以便在创建表单/控件对象时可用。如果之前找不到,那可能是导致绑定失败的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多