Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 装订不';不适用于自定义用户控件_C#_Wpf_Xaml - Fatal编程技术网

C# 装订不';不适用于自定义用户控件

C# 装订不';不适用于自定义用户控件,c#,wpf,xaml,C#,Wpf,Xaml,我有一个WPF UserControl库,其中包含要在WPF应用程序中使用的“自定义”控件: InputBox.xaml <UserControl x:Class="UserControls.BaseControls.InputBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.co

我有一个WPF UserControl库,其中包含要在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>
我希望能够在WPF应用程序中设置边框的borderbrushColor和textbox文本

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

如果在ViewModel中设置Testbind属性,则InputBox为空。如果我在输入框中写入内容并按enter键,则InputValue为null。 所以有一个绑定错误,但我不知道我的错误在哪里或什么地方


提前感谢您

如果删除FooViewModel对象的只读声明(如下所示),将无法工作。 私有FooViewModel _viewModel

请将用户控件中的文本框更改为以下内容:
 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. 

将文本框绑定更改为
Text=“{Binding ElementName=BaseInputBox,Path=InputValue,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}”
。这应该会解决问题。我的ViewModel会按照我的需要获取InputBox的值。但是,如果我在ViewModel本身中更改了属性,但我的InputBox没有更新……我无法重现您的问题。如果没有一个好的代码,您是否实现了INotifyPropertyChanged
,则无法确定错误是什么。但可以肯定的是,它与
\u viewModel
字段为
只读
,甚至与它为
私有
无关。
<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>
   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;
            }
        }
 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.