私はかなり新しいWPFですが、私が望むようにデータバインディングを取得するにはいくつかの問題があります。私はTextBoxを含むユーザーコントロールを作成しました。そのTextBoxのプロパティは、自分自身のUserControlのプロパティにバインドしたいので、別のものに再度バインドします。WPFのユーザーコントロールでデータバインディングを達成するには?
私には何が欠けていますか?
XAML
<!-- User Control -->
<TextBox Text="{Binding Path=TheText}" />
<!-- Window -->
<WpfApplication1:SomeControl TheText="{Binding Path=MyStringProp}" />
C#
// User Control ----
public partial class SomeControl : UserControl
{
public DependencyProperty TheTextProperty = DependencyProperty
.Register("TheText", typeof (string), typeof (SomeControl));
public string TheText
{
get
{
return (string)GetValue(TheTextProperty);
}
set
{
SetValue(TheTextProperty, value);
}
}
public SomeControl()
{
InitializeComponent();
DataContext = this;
}
}
// Window ----
public partial class Window1 : Window
{
private readonly MyClass _myClass;
public Window1()
{
InitializeComponent();
_myClass = new MyClass();
_myClass.MyStringProp = "Hallo Welt";
DataContext = _myClass;
}
}
public class MyClass// : DependencyObject
{
// public static DependencyProperty MyStringPropProperty = DependencyProperty
// .Register("MyStringProp", typeof (string), typeof (MyClass));
public string MyStringProp { get; set; }
// {
// get { return (string)GetValue(MyStringPropProperty); }
// set { SetValue(MyStringPropProperty, value); }
// }
}
よろしく
オリバーハナッピ
PS:私は私のユーザーコントロール上のINotifyPropertyChangedインターフェイスを実装しようとしましたが、それは助けになりませんでした。
私はUserControlのDataContextを 'this'に設定しています。したがって、Bindingはどのプロパティが意味されているかを知っています。テキストボックスの値もUserControlのプロパティに正しくバインドされていますが、データバインディングがWindowsで失われているようです。 –
ああそうです - 私は人々がそれを試みるのを見ました。私はそれがウィンドウレベルでバインディングを混乱させるものだと思う。 Element Bindingでそれを行うと、その必要がなくなります。 –
さて、私は今それを解決しました。問題は私のウィンドウでTwoWayにバインドモードを設定する必要があるということでした。ご協力ありがとうございました! –