双向绑定模式问题求教
本帖最后由 gaosam 于 2013-07-20 21:32:44 编辑 新手求教双向绑定的方式问题,不知道大虾们的使用思路是怎么样的
目前做的双向绑定思路一直是
/////////////////////////////////////////////////
后台创建一个public实体 然后给前台的Grid赋值
grid.DataContext = sbelistInfo;
然后Grid里的textbox双向绑定,点击保存的时候再拿sbelistInfo进行数据库commit的样子
当然textbox可以响应textchange事件把sbelistInfo的另一列的值更动,实现另一个控件的值直接变动的效果
这样做似乎有问题,但不影响效果
//////////////////////////////////////////////////
但这种思路用在datagrid上就出现问题了,比如datagrid列了个模板列
后台绑定了一个List<T> 数据源
我想实现对前几列的值改动后,自动算出合计列的值
<DataGridTemplateColumn Header="收费时间" Width="120" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<DatePicker Text="{Binding FFetchDate,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Background="Transparent" Width="115" SelectedDateChanged="DatePicker_SelectedDateChanged" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="A费用" Width="100" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<MyContrl:MyTextBox Text="{Binding lcPrice,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" TextChanged="MyTextBox_TextChanged" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="B费用" Width="100" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<MyContrl:MyTextBox Text="{Binding czPrice,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" TextChanged="MyTextBox_TextChanged" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="C费用" Width="100" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<MyContrl:MyTextBox Text="{Binding zxPrice,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" TextChanged="MyTextBox_TextChanged" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="杂费" Width="100" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<MyContrl:MyTextBox Text="{Binding qtPrice,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" TextChanged="MyTextBox_TextChanged" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="合计" Width="100" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<TextBlock Text="{Binding FAmount,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
private void DatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
if ((sender as DatePicker).Text == "")
{
t_CRM_ICStockBillEntryInfo selectItem = gdvDataList.SelectedItem as t_CRM_ICStockBillEntryInfo;
sbelist.Remove(selectItem);
}
else
{
t_CRM_ICStockBillEntryInfo sbe = sbelist.Where(q => q.FFetchDate == (sender as DatePicker).Text).FirstOrDefault();
if (sbe == null)
{
sbelist.Add(new t_CRM_ICStockBillEntryInfo { FFetchDate = (sender as DatePicker).Text, lcPrice = "0", czPrice = "0", zxPrice = "0", qtPrice = "0", FAmount = "0" });
}
}
gdvDataList.DataContext = null;//出现死循环
gdvDataList.DataContext = sbelist; //仅这样绑定 或者ItemSource也是,新添加进来的行不会显示出来
}
public class datasource : INotifyPropertyChanged
{
public bool ischeck=false;
public bool isCheck
{
get
{
return ischeck;
}
set
{
ischeck = value;
onPropertyChanged(this, "isCheck");
}
}
public string col1 { get; set; }
public string col2 { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void onPropertyChanged(object sender, string propertyName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
}
}
}