2011-04-28 66 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設置自會作出具有約束力的參考標籤對象本身不是控制的代碼隱藏。我希望這會有所幫助。