У меня есть следующий код в xaml:
<ListView Name="listView1" IsSynchronizedWithCurrentItem="True" >
<ListView.View>
<GridView>
<GridViewColumn Header="MyList">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Cost, Mode=TwoWay}"></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
в моем коде позади:
public partial class LogIn : UserControl, INotifyCollectionChanged
{
class Product
{
public string Name { get; set; }
public int Cost { get; set; }
}
ObservableCollection<Product> MyList = new ObservableCollection<Product>()
{
new Product(){ Cost=14},
new Product(){ Cost=15},
new Product(){ Cost=5},
new Product(){ Cost=20}
};
event NotifyCollectionChangedEventHandler INotifyCollectionChanged.CollectionChanged
{
add { throw new NotImplementedException(); }
remove { throw new NotImplementedException(); }
}
// constructor
public LogIn()
{
InitializeComponent();
listView1.DataContext = MyList;
}
private void button5_Click(object sender, RoutedEventArgs e)
{
this.MyList[0].Cost = 123456789;
// !!!!!!!!!!!!!!! I want the listview to update when I press this button
}
Просмотр списка не изменяется при обновлении последнего метода. Что мне нужно сделать, чтобы я мог обновить значения стоимости списка с кодом позади?
Редактировать
Благодаря SLaks я внес следующие изменения в свой класс Product, и он заработал.
public class Product : INotifyPropertyChanged
{
private int _cost;
public string Name { get; set; }
public int Cost
{
get
{
return _cost;
}
set
{
_cost = value;
OnPropertyChanged("Cost");
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private void OnPropertyChanged(string propertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
также я добавил следующую строку в конструктор usercontroll:
listView1.ItemsSource = MyList;