2011-12-20 48 views
18

我有兩個控件在WPF如何在控制本地屬性綁定在WPF

<Button HorizontalAlignment="Center" 
     Name="btnChange" 
     Click="btnChange_Click" 
     Content="Click Me" /> 

<Label Name="lblCompanyId" 
     HorizontalAlignment="Center" 
     DataContext="{Binding ElementName=_this}" 
     Content="{Binding Path=CompanyName}" /> 

正如我們可以看到,標籤綁定到本地屬性(在後面的代碼),我沒有看到任何值標籤,當我點擊按鈕...

下面是我的代碼背後...

public static readonly DependencyProperty CompanyNameProperty = 
    DependencyProperty.Register("CompanyName", typeof(string), typeof(Window3), new UIPropertyMetadata(string.Empty)); 

public string CompanyName { 
    get { return (string)this.GetValue(CompanyNameProperty); } 
    set { this.SetValue(CompanyNameProperty, value); } 
} 

private void btnChange_Click(object sender, RoutedEventArgs e) { 
    this.CompanyName = "This is new company from code beind"; 
} 

問候,

回答

30

嘗試

Content="{Binding ElementName=_this, Path=CompanyName}" 

沒有DataContext結合

編輯

我有你的代碼沒有問題,有一個名爲您的窗口x:Name="_this"

<Window x:Class="WpfStackOverflowSpielWiese.Window3" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Window3" 
     Height="300" 
     Width="300" 
     x:Name="_this"> 
    <Grid> 
    <StackPanel> 
     <Button HorizontalAlignment="Center" 
       Name="btnChange" 
       Click="btnChange_Click" 
       Content="Click Me" /> 

     <Label Name="lblCompanyId" 
      HorizontalAlignment="Center" 
      DataContext="{Binding ElementName=_this}" 
      Content="{Binding Path=CompanyName}"></Label> 

    </StackPanel> 
    </Grid> 
</Window> 

真的是你的窗戶Window3

public partial class Window3 : Window 
{ 
    public Window3() { 
    this.InitializeComponent(); 
    } 

    public static readonly DependencyProperty CompanyNameProperty = 
    DependencyProperty.Register("CompanyName", typeof(string), typeof(Window3), new UIPropertyMetadata(string.Empty)); 

    public string CompanyName { 
    get { return (string)this.GetValue(CompanyNameProperty); } 
    set { this.SetValue(CompanyNameProperty, value); } 
    } 

    private void btnChange_Click(object sender, RoutedEventArgs e) { 
    this.CompanyName = "This is new company from code beind"; 
    } 
} 

希望幫助

+0

謝謝punker76。你的解決方案工作我只需要添加x:Name =「_ this」和boooom。 – usergaro 2011-12-21 18:27:02

3

您正在結合您的標籤的DataContextButton,然後試圖設置它的ContentCompanyName,但是CompanyName不上Button

有效的屬性指定DataContext在您的綁定Path綁定到Button.DataContext.CompanyName而不是Button.CompanyName

另外,我建議你只是結合的內容,而不是兩者的結合在DataContext和內容

<Label Content="{Binding ElementName=btnChange, Path=DataContext.CompanyName}" /> 

如果你的代碼看起來酷似代碼示例發佈,那麼無論是ButtonLabel具有相同的DataContext,這樣你就可以直接綁定到CompanyName

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

編輯

剛注意到你的標籤的綁定是一個名爲_this的控件。我以爲它是Button,但我現在看到你的Button的名字是btnChange,而不是_this

雖然沒關係,答案依然如此。您正試圖綁定到UI控件的CompanyName屬性,該屬性不是有效的屬性。

+0

我看不到標籤的DataContext綁定到一個按鈕... – punker76 2011-12-20 19:19:24

+0

@ punker76看我的編輯。 '_this'必須引用一些UI控件,並且我錯誤地認爲它引用了Button,因爲它是代碼示例的一部分。校長仍然適用,即使它的控制不是按鈕。 – Rachel 2011-12-20 19:27:19

+0

在綁定中添加ElementName ...正是我一直在尋找的下午!謝謝! – thehelix 2017-12-08 02:01:28