Ton*_*Nam 5 c# data-binding wpf observablecollection inotifypropertychanged
以下是我输入数字时程序的行为方式:

我有一个列表视图绑定到一个可观察的集合.这是我的代码:(你可能会认为这部分类非常简单)
课程项目:
/// <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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码背后
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在xaml中的Listview:
<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>
Run Code Online (Sandbox Code Playgroud)
Col1=100它不会在列表视图中更新!还要注意总和是100而不是1000.我不希望column1大于某个数字x.在我的真实程序中,数字动态变化,我在Item类中计算它.
我怎样才能解决这个问题?
我找到了一些有趣的东西......如果我开始输入不同的数字,请看看会发生什么:我将在这个例子中输入5:

一旦它等于100就停止工作......
我成功了!!!我变了:
Text="{Binding Col1, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Run Code Online (Sandbox Code Playgroud)
为了:
Text="{Binding Col1, Mode=TwoWay}"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
475 次 |
| 最近记录: |