2016-08-31 4 views
-1

のために動作しませんバインディング:私はWPF-アプリケーションで使用する「カスタム」コントロールとWPF-ユーザーコントロール・ライブラリを持っているカスタムユーザーコントロール

InputBox.xaml

<UserControl x:Class="UserControls.BaseControls.InputBox" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      x:Name="BaseInputBox"> 
    <Grid> 
     <Border CornerRadius="10,0,10,0" 
       BorderThickness="1" 
       BorderBrush="{Binding ElementName=BaseInputBox, Path=InputColor}"> 
      <TextBox BorderThickness="0" 
        Background="Transparent" 
        VerticalAlignment="Center" 
        HorizontalContentAlignment="Center" 
        Text="{Binding ElementName=BaseInputBox, Path=InputValue}" /> 
     </Border> 

    </Grid> 
</UserControl> 

InputBox関数.xaml.cs

namespace UserControls.BaseControls 
{ 

    public partial class InputBox 
    { 
     public static readonly DependencyProperty InputColorProperty = DependencyProperty.Register("InputColor", typeof(Brush), typeof(InputBox), null); 
     public static readonly DependencyProperty InputValueProperty = DependencyProperty.Register("InputValue", typeof(string), typeof(InputBox), null); 

     public InputBox() 
     { 
      InitializeComponent(); 
     } 

     public string InputValue 
     { 
      get 
      { 
       return (string)GetValue(InputBox.InputValueProperty); 
      } 
      set 
      { 
       SetValue(InputBox.InputValueProperty, value); 
      } 
     } 

     public Brush InputColor 
     { 
      get 
      { 
       return (Brush)GetValue(InputBox.InputColorProperty); 
      } 
      set 
      { 
       SetValue(InputBox.InputColorProperty, value); 
      } 
     } 
    } 
} 

私はWPF-アプリケーションにおける境界線のborderbrushColorを設定する機能と、テキストボックス」テキストをしたい...

MainWindow.xaml(私のUserControl-ライブラリを参照する独立したプロジェクト)

<Window x:Class="DK.MathQuest.UI.WPF.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:UCs="clr-namespace:UserControls.BaseControls;assembly=UserControls"> 

    <DockPanel> 
     <UCs:InputBox InputValue="{Binding DataContext.Testbind, UpdateSourceTrigger=PropertyChanged}" 
         InputColor="Aqua" 
         Width="200" 
         Height="100" 
         DockPanel.Dock="Top" 
         KeyDown="TextBox_KeyDown" /> 
    </DockPanel> 
</Window> 

MainWindow.xaml.cs

private readonly FooViewModel _viewModel; 
     public MainWindow() 
     { 
      InitializeComponent(); 
      DataContext = new FooViewModel(); 
      _viewModel = (FooViewModel)DataContext; 
     } 


     private void TextBox_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (e.Key.Equals(Key.Enter)) 
      { 
       var input = (sender as InputBox).InputValue; 
       _viewModel.Testbind= input; 
      } 
     } 

私は私のViewModelで私のTestbindプロパティを設定するとInputBox関数は空です。私のInputBoxに何かを書いてEnterを押すと、InputValueはnullになります。 バインディングエラーがありますが、私のミスはどこにあるのかわかりません。

は、以下のようなようFooViewModelオブジェクトの読み取り専用の宣言を削除する場合、それは仕事をsholud事前に

+0

でINotifyPropertyChangedのを実装する必要があります。それはそれを修正する必要があります – lokusking

+0

私のViewModelは、InputBoxの値を取得します。しかし、ViewModel自体のプロパティを変更しても、私のInputBox 'は更新されません... – GrayFox

+0

問題を再現できません。あなたは 'INotifyPropertyChanged'を実装しました – lokusking

答えて

-2

、ありがとうございました。 プライベートFooViewModel _viewModel;

+2

良い[mcve]がなければ、バグが何であるかを知ることは不可能です。しかし、確かに、それは 'readonly'である' _viewModel'フィールドとは関係なく、 'private'であっても関係ありません。 –

0
Please change the textbox in user control to below   mentioned : 

    <TextBox BorderThickness="0" 
       Background="Transparent" 
       VerticalAlignment="Center" 
       HorizontalContentAlignment="Center" 
       Text="{Binding InputValue, RelativeSource     AncestorType=UserControl, Mode=FindAncestor}, UpdateSourceTrigger="PropertyChanged"} /> 

    Because InputValue property is in UserControl you have to bind it to input value on UserControl and it will work. 

また、あなたは `テキスト= "{のElementName = BaseInputBox、パス= inputValueで、モードをバインド=双方向、UpdateSourceTrigger =のPropertyChanged}"`にあなたのTextBox-バインドを変更のviewmodel

関連する問題