WPF 将文本框绑定到数据网格的当前选定行

发布于 2024-12-20 07:04:47 字数 1403 浏览 3 评论 0原文

我已经看过了,但找不到我要找的东西。我有一个MVVM环境。在视图模型上,我有一个可从数据库连接/查询的数据中获取的数据表。我已经将一个属性(getter/setter)公开为基于“TheTable.DefaultView”的“DataView”。

我在窗口中有一个数据网格绑定到数据视图......没问题。

<DataGrid AutoGenerateColumns="False" 
   Name="dataMyData"
   ItemsSource="{Binding Path=ViewModelViewProperty, 
   NotifyOnSourceUpdated=True, 
   NotifyOnTargetUpdated=True}"
   SelectedItem="{Binding Path=JustOneRecordInView, Mode=TwoWay}"
   SelectionMode="Single"
   SelectionUnit="FullRow"
   GridLinesVisibility="Horizontal"
   CanUserDeleteRows="False"
   CanUserAddRows="False" >

对于上面的“SelectedItem”,它也来自 ViewModel 通过其(getter/setter)公开的属性。

现在,我的问题。当我向下滚动数据网格中的记录列表时,我有其他文本框控件来显示比网格列表提供的更多数据。我希望能够编辑“当前行”的数据,因此我有一个文本框,其中包含我能想到的尽可能多的设置,但仍然有问题。

<TextBox 
   Text="{Binding Path=PropertyForCurrentRecord[SpecificColumnInDataViewRow], 
        Mode=TwoWay, 
        UpdateSourceTrigger=PropertyChanged, 
        ValidatesOnDataErrors=True, 
        ValidatesOnExceptions=True,
        BindsDirectlyToSource=True,
        NotifyOnSourceUpdated=True,
        NotifyOnTargetUpdated=True,
        NotifyOnValidationError=True}"
    Name="textBox1" VerticalAlignment="Top" Width="40" />

如果我滚动并处于数据编辑模式,并更改与当前行关联的文本框中的值,并且该值是网格中显示的列之一,则数据网格本身不会显示更改后的值。但是,如果我继续在同一记录上滚动并返回,则文本框中的值确实会显示更改后的值。

那么,当特定行中的各个列发生更改并且网格本身也更新时,如何强制网格数据源也被视为更新。谢谢...

I've looked and can't quite find what I'm looking for. I have a MVVM environment. On the View model, I have a Datatable available from data from a database connection/query. I've exposed a property (getter/setter) as a "DataView" based on "TheTable.DefaultView".

I have a datagrid in the window that binds to the dataview... no problem.

<DataGrid AutoGenerateColumns="False" 
   Name="dataMyData"
   ItemsSource="{Binding Path=ViewModelViewProperty, 
   NotifyOnSourceUpdated=True, 
   NotifyOnTargetUpdated=True}"
   SelectedItem="{Binding Path=JustOneRecordInView, Mode=TwoWay}"
   SelectionMode="Single"
   SelectionUnit="FullRow"
   GridLinesVisibility="Horizontal"
   CanUserDeleteRows="False"
   CanUserAddRows="False" >

For the "SelectedItem" above, it too comes from a property exposed on the ViewModel via its (getter/setter).

Now, my problem. As I scroll down the list of records in the data grid, I have other textbox controls to show more data than just the grid listing provides. I want to be able to edit the data of "the current row", so I have a textbox with as many settings as I can think of, but something is still amiss.

<TextBox 
   Text="{Binding Path=PropertyForCurrentRecord[SpecificColumnInDataViewRow], 
        Mode=TwoWay, 
        UpdateSourceTrigger=PropertyChanged, 
        ValidatesOnDataErrors=True, 
        ValidatesOnExceptions=True,
        BindsDirectlyToSource=True,
        NotifyOnSourceUpdated=True,
        NotifyOnTargetUpdated=True,
        NotifyOnValidationError=True}"
    Name="textBox1" VerticalAlignment="Top" Width="40" />

If I scroll though and am in an edit mode of the data, and change the value in the textbox associated with the current row, AND this value is one of the columns being displayed in the grid, the datagrid itself DOES NOT show the changed value. However, if I DO continue to scroll off and back on the same record, the value in the textbox DOES show the changed to value.

So, how can I force that the grid data source is considered updated too as the individual column from the specific row has changed, and the grid itself updated too. Thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咋地 2024-12-27 07:04:47

您的数据网格绑定到 SomeType 类型的某些元素的集合。要完成您的任务,您需要在 SomeType 中实现 INotifyPropertyChanged (或者从 ViewModelBase 继承,如果有的话)。您可以在此处查看一个很好的示例:http://www. hightech.ir/SeeSharp/best-implementation-of-inotifypropertychange-ever

Your datagrid is bound to a collection of some elements of type SomeType. To make your task you need to implement INotifyPropertyChanged in the SomeType (or inherit from ViewModelBase if you have it). You can take a look at a good sample here: http://www.hightech.ir/SeeSharp/best-implementation-of-inotifypropertychange-ever

南城旧梦 2024-12-27 07:04:47

哇......经过前几天的更多挖掘,我终于破解了它,这就是我修复它的方法。

private bool AcceptingTheChanges = false;
private DataRowView myRecord;
public DataRowView MyRecord
{
   get { return myRecord; }
   set {
          if (myRecord != null)
                myRecord.Row.Table.AcceptChanges();

          // Now, get the incoming value and re-store into private
          myRecord = value;
          // Finally, raise event that it changed to refresh window...
          RaisePropertyChanged("MyRecord");
       }
}

WOW... after a bunch more digging from the days prior, I finally cracked it and here is how I fixed it.

private bool AcceptingTheChanges = false;
private DataRowView myRecord;
public DataRowView MyRecord
{
   get { return myRecord; }
   set {
          if (myRecord != null)
                myRecord.Row.Table.AcceptChanges();

          // Now, get the incoming value and re-store into private
          myRecord = value;
          // Finally, raise event that it changed to refresh window...
          RaisePropertyChanged("MyRecord");
       }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文