Reputation: 301
I have a ListView
and when the user selects an item in the listview it will be displayed on a `TextBox for updating.
The problem is how can I prevent the SelectedItem
from updating when the user clears the text in the TextBox
C# Code:
private void Update_Button(object sender, RoutedEventArgs e)
{
string fname = tb_firstname.Text;
string lname = tb_lastname.Text;
var selectedEmployee = (Employee)lview.SelectedItem;
if (fname != null && lname != null)
{
selectedEmployee.FirstName = fname;
selectedEmployee.LastName = lname;
Refresh();
}
}
private void Refresh()
{
ICollectionView view = CollectionViewSource.GetDefaultView(employees);
view.Refresh();
}
private void lv_selectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedEmployee = (Employee) lview.SelectedItem;
if(selectedEmployee != null)
{
tb_firstname.Text = selectedEmployee.FirstName;
tb_lastname.Text = selectedEmployee.LastName;
}
}
XAML
<ListView ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" Height="93" Name="lview" SelectionChanged="lv_selectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}"></TextBlock>
<TextBlock Text="{Binding LastName}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
Upvotes: 0
Views: 117
Reputation: 10553
You should use the String.IsNullOrEmpty
function. You can find more info about it on MSDN.
if (!String.IsNullOrEmpty(fname) && !String.IsNullOrEmpty(lname))
{
...
}
Upvotes: 1
Reputation: 795
if (tb_firstname.Text != "")
{not empty code}
or
if (!string.empty(tb_firstname.Text))
{not empty code}
Upvotes: 1