2011-04-28 63 views
10

我在代码背后Text依赖属性代码反之亦然。数据绑定到后面

<Label Content="{Binding ???}" /> 

我该怎么办?

我已经做了一段时间之前,但现在我不记得如何 - 它是非常简单的。最简单的代码将被接受。

+0

我试过这个:'

回答

14

设置你的窗口/控制同一类的DataContext的,然后指定的结合,像这样的路径:

public class MyWindow : Window { 

    public MyWindow() { 
     InitializeComponents(); 
     DataContext = this; 
    } 

    public string Text { ... }  
} 

然后在您的xaml中:

<Label Content="{Binding Path=Text}"> 
+0

它的工作表示感谢。但为什么它没有在VS设计器中显示'Label'内容?! – drasto 2011-04-28 20:58:24

+0

从代码隐藏设置DataContext时,blend不会显示绑定到数据上下文的数据。您可以使用d:DataContext将设计时数据上下文设置为便于在混合中进行设计的另一个对象。看到这里:http://stackoverflow.com/questions/862829/what-is-the-advantage-of-setting-datacontext-in-code-instead-of-xaml – 2011-04-28 21:07:40

+0

我不想它在混合但在VisualStudio 。它是一样的吗? – drasto 2011-04-28 21:12:59

9

你必须设置窗口的DataContext使其工作。 XAML:

<Window x:Class="WpfApplication2.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" 
     DataContext="{Binding RelativeSource={RelativeSource Self}}"> 
    <Grid> 
     <StackPanel> 
     <Label Content="{Binding Text}" /> 
     <Button Content="Click me" Click="HandleClick" /> 
     </StackPanel> 

    </Grid> 
</Window> 

代码隐藏:

/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MainWindow), new PropertyMetadata("Hello world")); 
    public string Text 
    { 
     get { return (string)GetValue(TextProperty); } 
     set { this.SetValue(TextProperty, value); } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    protected void HandleClick(object sender, RoutedEventArgs e) 
    { 
     this.Text = "Hello, World"; 
    } 
} 
+0

-1复制代码粘贴到项目,它不起作用。 – drasto 2011-04-28 20:41:44

+2

我的不好,忘了DataContext。现在试试。抱歉。 (测试它,它的工作原理。) – 2011-04-28 21:01:03

+0

+1更好,但其他人更快,才可以编辑。无论如何,谢谢你的好例子,这对其他人很有用。 – drasto 2011-04-28 21:03:12

1

当你说它是在代码隐藏的时候,你的意思是它在你的类的Window的代码中?

您可能想要绑定到祖先类型为Window的RelativeSource。或者,如果您的数据上下文尚未设置,请在Load事件中将窗口的DataContext属性设置为窗口本身(this),然后使用{绑定文本}。

+0

yes代码隐藏意味着'Label'和'Text'依赖项属性在同一个类中。这里唯一的区别是'Label'在代码中是XAML和依赖属性。 – drasto 2011-04-28 20:53:25

+0

作品。谢谢。 – drasto 2011-04-28 20:57:22

0

在XAML设置的DataContext到代码隐藏可以是一个有点棘手,但一般来说,这些情况是最常见的:

  1. 你想使的DataContext的整个窗口或 定制用户控制

<Window 
blahhhh.. 
DataContext={Binding RelativeSource={RelativeSource Mode=Self}}> 

<UserControl 
Blahhhh.... 
DataContext={Binding RelativeSource={RelativeSource Mode=Self}}> 

2。如果设置了DataContext的窗口或用户控制比后面的代码别的什么,并有一个孩子的控制,你将需要设置它的的DataContext到代码的背后,您可以使用以下方法:

<Label DataContext={Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}/> 

定制用户控件

<Label DataContext={Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}/> 

在这种情况下,的DataContext设置自会作出具有约束力的参考标签对象本身不是控制的代码隐藏。我希望这会有所帮助。