2014-08-28 46 views
0

如何將TextBoxText屬性綁定到在代碼隱藏中定義的FoundationHeight clr-property。如何綁定到XAML中代碼隱藏中定義的屬性

XAML

<TextBox Text="{Binding FoundationHeight}"/> 

C#

public double FoundationHeight { get; set; } 
public AssignColumnPropertiesWindow() 
{ 
    InitializeComponent(); 
    FoundationHeight = 60; 
} 

回答

2

使用RelativeSource

如果隱藏代碼爲您的用戶控件定義:

<TextBox Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=FoundationHeight}" /> 
+0

謝謝非常。 – Vahid 2014-08-28 08:16:44

+1

不客氣;)確保你接受答案,如果它爲你工作得很好 – Damascus 2014-08-28 08:35:51

1

你可以這一行剛添加到窗口的構造

public AssignColumnPropertiesWindow() 
{ 
    InitializeComponent(); 

    DataContext = this; 

    FoundationHeight = 60; 
} 

和你Binding將工作

相關問題