无法获取内部XAML绑定以对抗依赖项属性

时间:2015-03-05 06:14:49

标签: c# wpf xaml dependency-properties data-binding

我有一个用户控件" CtrlComments",此控件具有以下XAML(它是超级基本的)。

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:wpftoolkit="http://schemas.microsoft.com/wpf/2008/toolkit" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
x:Name="ucRoot">
<Grid>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="ID: " />
        <TextBlock Text="{Binding Path=Deployment.Id}" />
    </StackPanel>
</Grid>

背后的代码如下,它是使控件正常运行的基础知识。关键是DependencyObject typeof(DeploymentDto),它有一个int属性Id,我们有兴趣根据上面的XAML绑定显示在我们的窗口上。

public partial class CtrlComments : UserControl, INotifyPropertyChanged
{
    public static readonly DependencyProperty DeploymentProperty =
        DependencyProperty.Register("Deployment", typeof(DeploymentDto),
                                    typeof(CtrlComments), new PropertyMetadata(new DeploymentDto()));

    public DeploymentDto Deployment
    {
        get
        {
            return (DeploymentDto)GetValue(DeploymentProperty);
        }
        set
        {
            SetValue(DeploymentProperty, value);
            OnPropertyChanged(new PropertyChangedEventArgs("Deployment"));
        }
    }

    public CtrlComments()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }
}

我们的问题是,尽管父控件和我的用户控件之间的绑定通过依赖属性工作(已验证)并且OnPropertyChanged方法触发,但我的XAML中的TextBlock不是& #39; t更新。

我注意到,当运行OnPropertyChanged方法时,事件处理程序为null,表示没有人通知存在属性更改。

我不明白为什么会这样。如果你能帮助解释我们哪里出错了,那将非常感激。

谢谢!

1 个答案:

答案 0 :(得分:2)

我试图复制你的问题,在这样做时,我认为我的问题出现在CtrlComments的以下一行:

this.DataContext = this;

删除此行只是让它对我有用。另请注意(正如@Aron在评论中所写),在OnPropertyChanged的setter中不应调用INotifyPropertyChanged的{​​{1}}。至少对我来说,根本没有必要实施INPC。

在您使用UserControl的XAML文件中,您很可能会设置另一个DependencyProperty(在更高级别,可能在DataContext中),因此我猜它不是'如果已经设置(或覆盖),则继承到用户控件。下面是我的工作代码,但也许我误解了你正在做的事情。如果是这种情况,请扩展您的问题以包括您如何使用UserControl,因为如果这不起作用,这是回答问题的关键:)

CtrlComments.xaml:

Window

CtrlComments.xaml.cs:

<UserControl x:Class="WpfApplication1.CtrlComments"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
  <Grid>
    <StackPanel Orientation="Horizontal">
      <TextBlock Text="ID: "/>
      <TextBlock Text="{Binding Path=Deployment.Id}"/>
    </StackPanel>
  </Grid>
</UserControl>

MainWindow.xaml:

namespace WpfApplication1
{
  public partial class CtrlComments : UserControl
  {
    public static readonly DependencyProperty DeploymentProperty =
      DependencyProperty.Register("Deployment", typeof(DeploymentDto), typeof(CtrlComments), new PropertyMetadata(new DeploymentDto { Id = 5 }));

    public DeploymentDto Deployment
    {
      get { return (DeploymentDto)GetValue(DeploymentProperty); }
      set
      {
        SetValue(DeploymentProperty, value);
      }
    }

    public CtrlComments()
    {
      InitializeComponent();
    }
  }
}

MainWindow.xaml.cs:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:WpfApplication1"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
  <StackPanel>
    <local:CtrlComments x:Name="testUC" Height="100" Deployment="{Binding Deployment}"/>
    <Button Click="Button_Click" Height="50" Width="100"/>
  </StackPanel>
</Window>

DeploymentDto:

namespace WpfApplication1
{
  public partial class MainWindow : Window, INotifyPropertyChanged
  {
    public MainWindow()
    {
      InitializeComponent();
    }

    private DeploymentDto deployment = new DeploymentDto { Id = 2 };
    public DeploymentDto Deployment
    {
      get { return deployment; }
      set { deployment = value; OnPropertyChanged("Deployment"); }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
      Deployment = new DeploymentDto { Id = new Random().Next(100) };
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propName)
    {
      if (PropertyChanged != null)
      {
        PropertyChanged(this, new PropertyChangedEventArgs(propName));
      }
    }
  }
}

public class DeploymentDto { public int Id { get; set; } } 绑定到其代码隐藏非常难看,但由于它仅用于示例目的,我希望它没问题:)