###前提・実現したいこと
wpfでバインド処理がうまくいきません。
やりいたこと
・下記ユーザーコントロールSubPanelを作成し、MainWindowにいくつか配置する。
・各SubPanelのテキストにViewModelで定義したText1、Text2・・・をBindingする。
###発生している問題・エラーメッセージ
型SubPanelのmyTextプロパティでBindingを設定することはできません。
BindingはDependencyObjectのDependencyPropertyでのみ設定できます。
上記警告がMainWindow.xamlのmyTextの部分で発生しておりうまくいきません。。。
コンパイルは通るのですが実行すると上記の部分で例外が発生します。
###該当のソースコード
xml
1SubPanel.xmal 2 3<UserControl x:Class="SubPanel " 4 ・ 5 ・ 6 ・ 7 8 <TextBox x:Name="Value"> 9 <TextBox.Style> 10 <Style TargetType="TextBox"> 11 <Setter Property="Text" Value="{Binding myText, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"/> 12 </Style> 13 </TextBox.Style> 14 </TextBox> 15</UserControl>
C#
1SubPanel.xaml.cs 2 3public partial class SubPanel : UserControl 4{ 5 public SubPanel () 6 { 7 InitializeComponent(); 8 } 9 public string myText { get; set; } 10} 11 12
xml
1MainWindow.xaml 2 3<Window x:Class="MainWindow" 4 ・ 5 ・ 6 ・ 7 8 <avalonDock:LayoutAnchorable Title="SubPanel"> 9 <StackPanel> 10 <local:SubPanel x:Name="aaaa" myText="{Binding Text1}"/> 11 <local:SubPanel x:Name="bbbb" myText="{Binding Text2}"/> 12 ・ 13 ・ 14 </StackPanel> 15 </avalonDock:LayoutAnchorable> 16</Window>
C#
1MainWindow.xaml.cs 2 3public partial class MainWindow : Window 4{ 5 public MainWindow() 6 { 7 InitializeComponent(); 8 this.DataContext = new ViewModel(this); 9 } 10} 11 12
###補足情報(言語/FW/ツール等のバージョンなど)
Windows8,1 pro 64bit
visual studio Community 2015
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/03 01:10