2008-11-25 26 views
6

我刚刚意识到我一直强制绑定/依赖属性,并没有真正从根本上理解这个概念。WPF DependencyProperties

赫雷什依赖属性:

public string Problem 
{ 
    get { return (string)GetValue(ProblemProperty); } 
    set { SetValue(ProblemProperty, value); } 
} 

public static readonly DependencyProperty ProblemProperty = 
    DependencyProperty.Register(
    "Problem", 
    typeof(string), 
    typeof(TextBox)); 

的XAML是这样:

<TextBlock Text="{Binding Path=Problem}"/> 

我的Problem属性手动设置到对象的构造函数值,但它不” t相应地更新TextBlock。 。 。有任何想法吗?我试过Mode="OneWay"Mode="TwoWay"关于绑定,它仍然无法正常工作。

我认为这应该是自动工作?或者我从根本上搞错了什么?

谢谢

+0

一切看起来都很好。它必须是你的`DataContext`。你如何设置它? – 2008-11-25 18:05:11

回答

11

你的问题绝对与你的DataContext有关。 {Binding}扩展需要知道您绑定的属性在哪里。它看起来的默认位置是元素DataContext,默认情况下它始终设置为其父元素的DataContext。如果您将DataContext沿着逻辑树走向父窗口,则DataContext将为空(因为您的窗口的DataContext为空)。因此,你的文本块上的{Binding}是“将我的Text属性绑定到我的DataContext的问题属性,它是空的。”

有几种方法可以解决这个问题。苡提到并设置你的元素属性绑定到指向其中的DependencyProperty是这样定义的窗口:

<TextBlock Text="{Binding Path=Problem,ElementName=_window}" /> 

另一种选择是设置你的窗口的DataContext的指向本身这样所有的包含在其内容中的元素将全部具有该窗口的DataContext。

<Window .... 
     DataContext="{Binding RelativeSource={RelativeSource Self}}"> 

现在,只要您需要绑定在窗口中定义(如您的问题依赖属性)的属性,那么你可以这样做:

<TextBlock Text="{Binding Problem}" /> 
+0

设置窗口datacontext的解决方案本身并不适用于我。 它只适用于将一个或更多级别的元素(第一个网格工作)的元素的datacontext设置为窗口。 这是预期的吗? – Erik83 2016-02-26 13:16:42

0

它是一个窗口,这是在设置

public partial class Window1 : Window 
{ 
    public string Problem 
    { 
     get { return (string)GetValue(ProblemProperty); } 
     set { SetValue(ProblemProperty, value); } 
    } 

    public static readonly DependencyProperty ProblemProperty = 
        DependencyProperty.Register(
        "Problem", 
        typeof(string), 
        typeof(Window1)); 


    public Window1() 
    { 
     InitializeComponent(); 

     Problem = "ifowiof"; 
    } 

    public void OnClick(object sender, EventArgs e) 
    { 
     Problem = "behl"; 
    } 

    public void OnCancel(object sender, EventArgs e) 
    { 
     Problem = "eioeopje"; 
    } 
} 

XAML:

<Window x:Class="WpfToolTip.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
     <StackPanel> 
      <Button Click="OnClick" Content="OK" /> 
      <Button Click="OnCancel" Content="Cancel" /> 
      <TextBlock Text="{Binding Path=Problem}" /> 
    </StackPanel> 
</Window> 

它如果我设置RelativeSource就像您在加载时所说的那样工作,但是如果我手动更改代码中的Problem属性(即。通过一个按钮点击)它从不更新TextBlock与新的价值。

+0

你有没有找到答案?我有类似的问题。当您设置问题属性时,绑定似乎会丢失。我不确定以这种方式更新值的正确程序是什么。 – 2008-12-19 09:18:25

2

您可以在这里使用ElementName绑定,元素将是Window本身。

<Window x:Class="WpfToolTip.Window1" 
x:Name="_window" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <StackPanel> 
     <Button Click="OnClick" Content="OK" /> 
     <Button Click="OnCancel" Content="Cancel" /> 
     <TextBlock Text="{Binding Path=Problem,ElementName=_window}" /> 
</StackPanel> 

0

在你的代码注册依赖属性的类文本框(报价的最后一行)。

公共静态只读的DependencyProperty ProblemProperty =
DependencyProperty.Register(
“问题”,
typeof运算(字符串),
typeof运算(文本框));

因此,您可以为ProblemProperty设置仅用于文本框的值,但我无法在任何代码片段中找到任何文本框。 您应该从样本中注册您的依赖项属性,该值将被赋值给哪个类型,正确的选择对我来说并不明显。 您可以像Micah那样将其定义为窗口的DP,然后在您的实例化窗口上设置该属性。或者你可以将它定义到窗口内的任何命名的依赖对象,即用名称= m_ContentElement一些对象,然后设置您的结合
{Binding ElementName=m_ContentElement, Path=Problem}
或更短:
{Binding Problem, ElementName=m_ContentElement}

0

有两种方式来了解原因你描述的问题。

起初 - 你应该尝试设置属性更改处理程序(在依赖项属性声明中),并将断点放在那里。你会看到你的财产是否正在改变。

第二个 - 你应该检查依赖项属性所有者的类型。

您能否展示完整的xaml和代码隐藏?

2

尝试typeof(object)而不是typeof(string)

相关问题