我正在嘗試使具有IsReadOnly
屬性的Address
控件具有IsReadOnly
屬性,該屬性將使每個TextBox
僅在設置爲true時才被讀取。將屬性綁定到獨立於DataContext的Silverlight依賴項屬性
<my:AddressControl Grid.Column="1" Margin="5" IsReadOnly="True"/>
我已經設法做到這一點,只要有依賴項屬性,它的工作原理。
這裏的依賴屬性的簡單類中聲明:
public partial class AddressControl : UserControl
{
public AddressControl()
{
InitializeComponent();
this.DataContext = this;
}
public static readonly DependencyProperty IsReadOnlyProperty =
DependencyProperty.Register("IsReadOnly", typeof(bool),
typeof(AddressControl), null);
public bool IsReadOnly
{
get { return (bool)GetValue(IsReadOnlyProperty); }
set { SetValue(IsReadOnlyProperty, value); }
}
}
在XAML此代碼隱藏文件,我爲每個地址線Textbox
:
<TextBox IsReadOnly="{Binding IsReadOnly}" Text="{Binding City, Mode=TwoWay}"/>
<TextBox IsReadOnly="{Binding IsReadOnly}" Text="{Binding State, Mode=TwoWay}"/>
<TextBox IsReadOnly="{Binding IsReadOnly}" Text="{Binding Zip, Mode=TwoWay}"/>
就像我說的這個工程只是精細。 問題是,Address
控制本身綁定到它的父對象(我有幾個地址我綁定)。
<my:AddressControl DataContext="{Binding ShippingAddress, Mode=TwoWay}" IsReadOnly="True">
<my:AddressControl DataContext="{Binding BillingAddress, Mode=TwoWay}" IsReadOnly="True">
的問題是,當我設置DataContext
比'this'
其他東西則IsReadOnly
斷結合。不足爲奇,因爲它在Address
數據實體上尋找IsReadOnly
,並且它不存在或屬於那個數據實體。
我已經嘗試過幾乎所有binding attributes組合,以獲得IsReadOnly
綁定到AddressControl
obejct,但無法使其工作。
我試過這樣的事情,但我無法獲得IsReadOnly
獨立綁定到AddressControl
屬性,而不是它的DataContext
。
<TextBox IsReadOnly="{Binding RelativeSource={RelativeSource Self}, Path=IsReadOnlyProperty}" Text="{Binding City, Mode=TwoWay}" />
我覺得我很親密。我究竟做錯了什麼?
玩過'Text'屬性的綁定工具後,似乎'RelativeSource Self'實際上意味着綁定到TextBox本身 - 這不是我想要的。因此,我認爲我需要FindAncestor,它在Silverlight中不存在:-(我認爲我卡住了?http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID = 480603 – 2009-12-16 12:58:16