C# 将两个控件同时绑定到两个源
我正在尝试将textbox绑定到字符串变量,同时将ComboBox绑定到列表C# 将两个控件同时绑定到两个源,c#,wpf,C#,Wpf,我正在尝试将textbox绑定到字符串变量,同时将ComboBox绑定到列表 <Window x:Class="Assignment2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:validators="clr-namespace:Assignment2" xmlns:x="http://schemas
<Window
x:Class="Assignment2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:validators="clr-namespace:Assignment2"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="109,103,0,0" Name="StringComboBox" VerticalAlignment="Top" Width="120" SelectionChanged="SelectionChanged">
<ComboBox.ItemsSource>
<Binding Path="ListString" Mode="TwoWay" UpdateSourceTrigger="LostFocus"></Binding>
</ComboBox.ItemsSource>
</ComboBox>
<TextBox Height="23" HorizontalAlignment="Right" Margin="0,51,250,0" Name="inputTextBox" VerticalAlignment="Top" Width="154" LostFocus="inputTextBox_LostFocus">
<TextBox.Text>
<Binding Path="Name1" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<validators:RequiredFieldValidator ErrorMessage="This field Should not be empty"></validators:RequiredFieldValidator>
<validators:OnlyCharacterValidation ErrorMessage="Only characters allowed"></validators:OnlyCharacterValidation>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</Grid>
</Window>
背后的代码是:
public partial class MainWindow : Window, INotifyPropertyChanged
{
string _name = "Default Value";
public ObservableCollection<string> ListString;
public string Name1
{
get { return _name; }
set
{
_name = value;
}
}
public MainWindow()
{
ListString = new ObservableCollection<string>();
ListString.Add("AAA");
ListString.Add("BBB");
ListString.Add("CCC");
ListString.Add("DDD");
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
private void inputTextBox_LostFocus(object sender, RoutedEventArgs e)
{
OnPropertyChanged("Name1");
}
}
}
public分部类主窗口:窗口,INotifyPropertyChanged
{
字符串_name=“默认值”;
公共可观察收集列表字符串;
公共字符串名称1
{
获取{return\u name;}
设置
{
_名称=值;
}
}
公共主窗口()
{
ListString=新的ObservableCollection();
添加(“AAA”);
添加(“BBB”);
添加(“CCC”);
添加(“DDD”);
初始化组件();
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
私有void inputTextBox\u LostFocus(对象发送方,路由目标e)
{
不动产变更(“名称1”);
}
}
}
我似乎不能同时绑定Combobox和Textbox。我已经尝试了很多解决方法。但无论哪种方式,只有其中一种有效。
有什么简单的解决办法吗
public string Name1
{
get { return _name; }
set
{
_name = value;
}
}
忘记OnPropertyChanged(“Name1”)代码>
此外:
public ObservableCollection<string> ListString;
您的属性应该调用OnPropertyChanged
处理程序,如下所示:
public string Name1
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name1");
}
}
private ObservableCollection<string> listString = new ObservableCollection<string>();
public ObservableCollection<string> ListString
{
get { return listString ; }
set
{
listString = value;
OnPropertyChanged("ListString");
}
}
公共字符串名称1
{
获取{return\u name;}
设置
{
_名称=值;
不动产变更(“名称1”);
}
}
private ObservableCollection listString=新的ObservableCollection();
公共ObservableCollection列表字符串
{
获取{return listString;}
设置
{
listString=值;
OnPropertyChanged(“ListString”);
}
}
请仔细查看MSDN页面中的示例。尝试更改此示例
public string Name1
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name1");
}
}
public string Name1
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name1");
}
}
private ObservableCollection<string> listString = new ObservableCollection<string>();
public ObservableCollection<string> ListString
{
get { return listString ; }
set
{
listString = value;
OnPropertyChanged("ListString");
}
}
public string Name1
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name1");
}
}