Reputation: 3941
I am trying bind textbox to String variable same time I want to bind ComboBox to List.
<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>
Code Behind is:
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");
}
}
}
I can't seem to bind Combobox and Textbox at the same time. I have tried many solutions to this. but either way only one of these works. Is there any simple solution to this?
Upvotes: 0
Views: 116
Reputation: 2759
Try change to this.
public string Name1
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name1");
}
}
Upvotes: 1
Reputation: 69959
Your properties should call the OnPropertyChanged
handler like this:
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");
}
}
Please look carefully at the example in the INotifyPropertyChanged Interface page on MSDN.
Upvotes: 1
Reputation: 2430
public string Name1
{
get { return _name; }
set
{
_name = value;
}
}
Forgot OnPropertyChanged("Name1");
?
Also :
public ObservableCollection<string> ListString;
Your ListString should be a Property, not just a field :
public ObservableCollection<string> ListString { get; set; }
Finally, I don't see the reason for this :
private void inputTextBox_LostFocus(object sender, RoutedEventArgs e)
{
OnPropertyChanged("Name1");
}
Upvotes: 0