我有綁定到我的依賴項新控件的屬性問題。
我決定寫一些測試來研究這個問題。
綁定到的DependencyProperty沒有結果
從TextBox.Text綁定到另一個TextBox.Text
XAML代碼:
<TextBox Name="Test" Text="{Binding ElementName=Test2, Path=Text, UpdateSourceTrigger=PropertyChanged}" />
<TextBox Name="Test2" Grid.Row="2" />
結果是好 - 當我在寫第一個TextBox的東西 - >第二個文本框正在更新(相反也是)。
我創造了新的控制 - >例如 「SuperTextBox」 與依賴屬性 「SuperValue」。
控制XAML代碼:
<UserControl x:Class="WpfApplication2.SuperTextBox"
...
Name="Root">
<TextBox Text="{Binding SuperValue, ElementName=Root, UpdateSourceTrigger=PropertyChanged}" />
</UserControl>
後面的代碼:
public partial class SuperTextBox : UserControl
{
public SuperTextBox()
{
InitializeComponent();
}
public static readonly DependencyProperty SuperValueProperty = DependencyProperty.Register(
"SuperValue",
typeof(string),
typeof(SuperTextBox),
new FrameworkPropertyMetadata(string.Empty)
);
public string SuperValue
{
get { return (string)GetValue(SuperValueProperty); }
set { SetValue(SuperValueProperty, value); }
}
}
好了,現在測試!
從TextBox.Text綁定到SuperTextBox.SuperValue
<TextBox x:Name="Test1" Text="{Binding ElementName=Test2, Path=SuperValue, UpdateSourceTrigger=PropertyChanged}" />
<local:SuperTextBox x:Name="Test2" Grid.Row="2"/>
測試是正確的呢! 當我在TextBox中寫東西時,SuperTextBox正在更新。 當我在SuperTextBox中寫入時,TextBox正在更新。 一切都好!
現在一個問題:
從SuperTextBox.SuperValue綁定到TextBox.Text
<TextBox x:Name="Test1"/>
<local:SuperTextBox x:Name="Test2" SuperValue="{Binding ElementName=Test1, Path=Text, UpdateSourceTrigger=PropertyChanged}" Grid.Row="2"/>
在這種情況下,當我在寫東西SuperTextBox,文本框沒有更新!
我該如何解決這個問題?
PS:問題是非常非常長,我很抱歉,但我想準確地描述了我的問題。
謝謝!現在一切都很清楚。 :) – Never
哦,另一個案件仍然無法正常工作。你可以檢查新版本的問題 - 我添加了「Edit1」部分。 – Never
對不起,但我認爲這兩個問題都非常相似,不需要創建新的「主題」。 我被刪除了「接受的答案」,因爲它可能造成「主題結束」的錯覺。 但是好的,對不起。 – Never