Here is how my program behaves as I type numbers:
I have a listview that is binded to an observable collection. Here is my code: ( you may skeep this part the classes are very simple)
Class Item:
/// <summary>
/// Represent each row in listview
/// </summary>
public class Item : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
void UpdateSum()
{
Sum = Col1;// + col2 + col3 etc
}
decimal _Col1;
public decimal Col1 // ||
{ // ||
get // ||
{ // ||
return _Col1; // ||
} // ||
set // ||
{ // \ || /
if (value > 100) // \ || /
{ // \/
Col1 = 100; // !!!!!!!!!!!!!!!!!!!!! HERE why does the listview does't update!!!!!!!!
NotifyPropertyChanged("Col1");
}else
{
_Col1 = value;
}
UpdateSum();
NotifyPropertyChanged("Col1");
}
}
decimal _Sum;
public decimal Sum
{
get
{
return _Sum;
}
set
{
_Sum = value;
NotifyPropertyChanged("Sum");
}
}
}
Code Behind
using System;
using System.Windows;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace WpfApplication3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public ObservableCollection<Item> Collection = new ObservableCollection<Item>();
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Collection.Add(new Item());
listView2.DataContext = Collection;
listView2.ItemsSource = Collection;
listView2.IsSynchronizedWithCurrentItem = true;
}
}
}
Listview in xaml:
<ListView Name="listView2" >
<ListView.View>
<GridView>
<GridViewColumn Header="Column1" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Width="200" Text="{Binding Col1, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Sum" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Width="200" Text="{Binding Sum, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Col1=100
it does not update in the listview! Also note how the sum comes to be 100 and not 1000.I don't want column1 to be greater than some number x. In my real program that number changes dynamically and I calculate it inside the Item class.
How can I fix this?
I found something interesting... If I start typing different numbers taks a look at what happens: I will just type 5 in this example:
once it is equal to 100 it stops working...
I made it work!!! I changed:
Text="{Binding Col1, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
for:
Text="{Binding Col1, Mode=TwoWay}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With