C# 将文本框数据绑定到代码隐藏中的字符串失败
我正在尝试将字符串属性绑定到XAML中TextBox的文本字段。我在用户控件中执行此操作。我搜索了StackOverflow和internet,找到了各种相关主题,例如:C# 将文本框数据绑定到代码隐藏中的字符串失败,c#,wpf,xaml,data-binding,wpf-controls,C#,Wpf,Xaml,Data Binding,Wpf Controls,我正在尝试将字符串属性绑定到XAML中TextBox的文本字段。我在用户控件中执行此操作。我搜索了StackOverflow和internet,找到了各种相关主题,例如: 我已经尽可能地遵循了这些示例中的代码,但是Input属性似乎没有绑定到TextBox。我尝试了各种不同的方法来设置DataContext,包括从代码隐藏中设置,但仍然不起作用 我遗漏了什么,这是一个问题,因为它是一个用户控件吗 调用SearchInputTextBox\u TextChanged事件中的代码,但始终输出
SearchInputTextBox\u TextChanged
事件中的代码,但始终输出空字符串。如果我将Debug.WriteLine
调用放在输入集部分,则什么也不会发生
XAML文件:
<UserControl x:Class="DatabaseViewerApp.View.SearchBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="70" d:DesignWidth="240"
x:Name="Control">
<Border Padding="5" Background="#303030">
<StackPanel>
<TextBlock Text="Search" Margin="0,0,0,0" FontSize="20px" Foreground="White"></TextBlock>
<TextBox Name="SearchInputTextBox" Text="{Binding ElementName=Control, Path=Input}" Margin="0,5" FontSize="15px" TextChanged="SearchInputTextBox_TextChanged"></TextBox>
</StackPanel>
</Border>
</UserControl>
C文件:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace DatabaseViewerApp.View
{
/// <summary>
/// Interaction logic for SearchBox.xaml
/// </summary>
public partial class SearchBox : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string input;
public string Input
{
get { return input; }
set
{
if (value != input)
{
input = value;
NotifyPropertyChanged("Input");
}
}
}
public void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public SearchBox()
{
InitializeComponent();
}
private void SearchInputTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
Debug.WriteLine(Input);
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统诊断;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间DatabaseViewerApp.View
{
///
///SearchBox.xaml的交互逻辑
///
公共部分类搜索框:UserControl,INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
私有字符串输入;
公共字符串输入
{
获取{返回输入;}
设置
{
如果(值!=输入)
{
输入=值;
NotifyPropertyChanged(“输入”);
}
}
}
public void NotifyPropertyChanged(字符串propertyName)
{
if(this.PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
公共搜索框()
{
初始化组件();
}
private void SearchInputTextBox_TextChanged(对象发送者,TextChangedEventArgs e)
{
Debug.WriteLine(输入);
}
}
}
试试这个:
<TextBox Name="SearchInputTextBox"
Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=UserControl},Path=Input}"
Margin="0,5" FontSize="15px" TextChanged="SearchInputTextBox_TextChanged">
</TextBox>
我认为你的问题不在于它不起作用。。。问题是您可能没有正确地测试它。您的代码在新的WPF解决方案中对我有效。您可能缺少的是,当您离开焦点时,文本绑定会得到更新。相反,文本一旦输入到文本框中,就会触发TextChanged事件 因此,发生的情况是,当触发TextChanged事件时,绑定尚未更新。如果您尝试在属性设置器中放置断点,然后更改文本框中的一些文本,并将焦点移出该控件,您应该会看到它被击中
另一方面,将文本绑定到代码背后的属性没有多大意义,因为可以直接从那里访问UI元素。非常感谢!我在谷歌上搜索了你的建议,找到了以下内容:,然后将
UpdateSourceTrigger=PropertyChanged
添加到文本框的绑定中,它工作正常。