I have an annoying problem that I have been struggling with for hours. I have a simple Listview application on which I want to change the customer name by typing a new name and sending it via button click. The listview looks fine and I see in the code behind (debug) that the value for new names changes but my cursed lisview keeps the old name.
namespace WpfTurtorial
{
public class DemoCustomer: INotifyPropertyChanged
{
public DemoCustomer SelectedCustomer { get; set; }
private int idValue;
private string customerNameValue = String.Empty;
private string phoneNumberValue = String.Empty;
public string newCustName = String.Empty;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
/*
private DemoCustomer()
{
customerNameValue = "Customer";
phoneNumberValue = "(312)555-0100";
}
public static DemoCustomer CreateNewCustomer()
{
return new DemoCustomer();
}
*/
public int ID
{
get
{
return this.idValue;
}
set
{
this.idValue = value;
}
}
public string CustomerName
{
get
{
return this.customerNameValue;
}
set
{
if (value != this.customerNameValue)
{
this.customerNameValue = value;
NotifyPropertyChanged();
}
}
}
public string PhoneNumber
{
get
{
return this.phoneNumberValue;
}
set
{
if (value != this.phoneNumberValue)
{
this.phoneNumberValue = value;
NotifyPropertyChanged();
}
}
}
public string NewCustName
{
get
{
return this.newCustName;
}
set
{
if (value != this.newCustName)
{
this.newCustName = value;
NotifyPropertyChanged();
}
}
}
}
}
`
```
namespace WpfTurtorial
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ObservableCollection<DemoCustomer> _items;
public ObservableCollection<DemoCustomer> Items
{
get
{
if (_items == null)
{
_items = new ObservableCollection<DemoCustomer>();
}
return _items;
}
set
{
_items = value;
}
}
private ICommand _command;
public DemoCustomer SelectedCustomer { get; set; }
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
ChangeCustomer();
}
private void ChangeCustomer()
{
Items = new ObservableCollection<DemoCustomer>
{
new DemoCustomer { ID = 1234, CustomerName = "Customer1", PhoneNumber = "1234-9876543", NewCustName = "" },
new DemoCustomer { ID = 1235, CustomerName = "Customer2", PhoneNumber = "1234-9876544", NewCustName = "" }
};
}
public ICommand Command
{
get { return _command ?? (_command = new RelayCommand(x => { SetCustomerName(x.ToString()); })); }
}
private static void SetCustomerName(string names)
{
MessageBox.Show("Responding to click event on " + names);
DemoCustomer demo = new DemoCustomer();
demo.CustomerName = names;
}
private void ListView1_Loaded(object sender, RoutedEventArgs e)
{
ListView1.Items.Refresh();
}
}
}
`
```
<Window x:Class="WpfTurtorial.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfTurtorial"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="199*" />
<RowDefinition Height="18*"/>
</Grid.RowDefinitions>
<ListView x:Name="ListView1" Grid.Row="0" Margin="5" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedCustomer}" Loaded="ListView1_Loaded">
<ListView.View>
<GridView>
<GridViewColumn x:Name="ID" Header="ID" Width="80" DisplayMemberBinding="{Binding Path=ID}" />
<GridViewColumn x:Name="CustomerName" Header="CustomerName" Width="230" DisplayMemberBinding="{Binding Path=CustomerName}" />
<GridViewColumn Header="PhoneNumber" Width="130" DisplayMemberBinding="{Binding Path=PhoneNumber}" />
<GridViewColumn x:Name="NewName" Header="New Name" Width="230">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="tbCustName" Width="200" Text="{Binding Path=NewCustName}"></TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn x:Name="btn1" Header="Set" Width="130">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Width="80" Height="22" Margin="1"
Command="{Binding ElementName=ListView1, Path=DataContext.Command}"
CommandParameter="{Binding Path=NewCustName}">
<TextBlock Text="Change Name" />
</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
`
I have tried all forms of dependencies to update the listview. All forms of help are gratefully received