绑定不适用于自定义UserControl

时间:2016-08-31 06:27:56

标签: c# wpf xaml

我有一个带有' custom'的WPF-UserControl-Library。我希望在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);
            }
        }
    }
}

我希望能够设置边框的borderbrushColor和textbox&#39; WPF应用程序中的文本......

MainWindow.xaml(引用我的UserControl-Library的独立项目)

<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。 所以有一个绑定错误,但我不知道我的错误在哪里或什么。

提前谢谢

2 个答案:

答案 0 :(得分: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. 

Also you need to implement InotifyPropertyChanged in viewmodel

答案 1 :(得分:-2)

如果删除FooViewModel对象的只读声明,它会破坏工作,如下所示。 private FooViewModel _viewModel;