使用WPF用户控件进行双向数据绑定 [英] Two way databinding with WPF user control

查看:127
本文介绍了使用WPF用户控件进行双向数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个带有WPF数据网格的用户控件.我在使用元素主机的Windows窗体中使用了此用户控件.我可以使用ListCollectionView将数据绑定到用户控件,但是当我对datagrid进行更新时,所做的更改不会反映回来.

我设置了Mode = TwoWay,但没有用.
有任何想法吗?这是我的代码示例:

Hi,
I have a user control with a WPF Data Grid. I used this user control in a Windows form using Element Host. I can bind the data to the user control using ListCollectionView but when i make updates to the datagrid the changes are not reflected back.

I set the Mode = TwoWay but no use.
Any ideas? Here is a sample of my code:

UserControl.xaml
<my:DataGrid
ItemsSource="{Binding}"
HorizontalScrollBarVisibility="Hidden" SelectionMode="Extended"
CanUserAddRows="False" CanUserDeleteRows="False"
CanUserResizeRows="False" CanUserSortColumns="False"
AutoGenerateColumns="False"
RowHeaderWidth="20" RowHeight="25" Width="Auto" Height="Auto"
RowStyle="{StaticResource RowSelected1}"
CellStyle="{StaticResource RowSelected}"
GridLinesVisibility="Horizontal" >
<my:DataGrid.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle1}">
<GroupStyle.Panel>
<ItemsPanelTemplate>
<my:DataGridRowsPresenter/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</my:DataGrid.GroupStyle>
<my:DataGrid.Columns>
<Controls:LabelTextBoxColumn
Header="Tread BarCode" Width="2*"
HorizontalAlignment="Left" VerticalAlignment="Center"
ElementStyle="{StaticResource BaseLabelCellStyle}"
EditingElementStyle="{StaticResource BaseTextBoxCellStyle}"
Binding="{Binding Name,Mode=TwoWay}"/>
//The code in my Windows form frm1 is :
this.sdaTrueName.Fill(this.dstrueSrch1.dtTrue);
view = new ListCollectionView(this.dstrueSrch1.dtTrue.ToList());
view.GroupDescriptions.Add(new PropertyGroupDescription("Name"));
UserControlABC.DataContext = view;


我必须将数据保存回数据库中.我正在使用数据集和数据表.

请帮我任何想法吗?


I have to save the data back into my database.I am using Dataset and DataTables.

Please help me with any ideas ?

推荐答案


由于您正在使用List<> ,则不会触发添加,删除通知,因此即使您更新了数据源也不会更新目标.会触发添加,删除通知本身并相应地更新视图.否则,您将显式更新源和目标,例如:

Hi,
Since you are using the List<> , the add,remove notifications are not triggered,therefore the target is not updated eventhough you updated the datasource.There is a class ObservableCollection<> that triggers the add , remove notifications itself and update the view accordingly.Otherwise you update the source and target explicitly , for eg:

BindingExpression expression = BindingOperations.GetBindingExpression(thedatagrid, DataGrid.ItemsSourceProperty);
     if (null != expression)
     {
       expression.UpdateSource();
     }


这篇关于使用WPF用户控件进行双向数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆