WPF中的Datagrid不会刷新 [英] Datagrid in WPF does not get refreshed

查看:243
本文介绍了WPF中的Datagrid不会刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经创建了一个WPF应用程序.我有一个文本框,在其中提供了输入,单击按钮后,输入将保存到数据库中,在旁边有一个Datagrid,它将填充数据库中的数据.现在,当我添加新数据时,它将存储在数据库中,但不会反映在datagrid上.
我想我必须刷新数据网格,所以我使用了DataGrid.Items.Refresh(),但这不适用于我的应用程序.
请帮助解决该问题.
谢谢

代码段:

Hi,
I have created a WPF application. I have a textbox in which I give input which is saved to database on button click, adjacently I have a Datagrid present which will populate the data in database. Now when I add new data it gets stored in database but is not reflected on datagrid.
I guess I will have to refresh the datagrid so I used DataGrid.Items.Refresh() but this does not work for my application.
Plz help how to solve this issue.
Thanks

Code Snippet:

private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
//Here is the code to save the data to XML
}



我的datagrid在wpf .xaml文件中按以下方式绑定:



My datagrid gets bind as follows in wpf .xaml file :

<Window.Resources>
        <XmlDataProvider Source="C:\Output.xml"  

                         XPath="Data" x:Key="DataFile" />
    </Window.Resources>

<DataGrid AutoGenerateColumns="False" Height="177" HorizontalAlignment="Left" Margin="322,62,0,0" Name="dgData" VerticalAlignment="Top" Width="149" ItemsSource="{Binding Source={StaticResource DataFile},XPath=*}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Type" Binding="{Binding XPath=@Type}" />
                <DataGridTextColumn Header="Expense" Binding="{Binding XPath=@Value}" />
            </DataGrid.Columns>
        </DataGrid>



我的XML如下所示:



My XML looks as follows :

<Data>
   <Record>
     <Type>
       ABC
     </Type>
     <Expense>
       125
     </Expense>
   </Record>
   <Record>
     <Type>
       DEF
     </Type>
     <Expense>
       250
     </Expense>
   </Record>
</Data>



我只想在将数据添加到xml之后更新网格.



I just want to update the grid after the data is added to xml .

推荐答案

您的所有业务实体都必须实现INotifyPropertyChanged接口,以便进行UI通知.
第二,为了更好地表现,使用ObservableCollection.类在您的dataGrid中作为项目源对象.
第三,在Xaml中设置要遵循的TextBox绑定模式:Mode = TwoWay.
希望对您有所帮助.
All your business entities must implement INotifyPropertyChanged interface, for UI notification.
Second, for beter performance use ObservableCollection<> class as item source object, in your dataGrid.
Third, in Xaml set to TextBox binding mode to follow : Mode=TwoWay.
Hope this will help.


(我假设使用WPF,但是您同时拥有WPF和ASP.NET)

由于您使用的是静态资源,因此您无法在更新列表时通知系统.这意味着在加载表单之前,该列表几乎必须存在.我想这是你的问题.另一种选择是具有将用作ViewModel的类的实例化.数据更新后,创建一个新列表(必须进行更改以使INotifyPropertyChange起作用,否则PropertyChange将被忽略).
(I am assuming WPF, but you have both WPF and ASP.NET)

Because you are using a static resource, there is no way to notify the system when you update the list. This means that the list pretty much has to be in place before you load the form. I would guess that this is your problem. Another option would be to have a instantiation of a class that will serve as the ViewModel. When the data gets updated, create a new list (have to have a change to have INotifyPropertyChange work, otherwise the PropertyChange will just be ignored).


这篇关于WPF中的Datagrid不会刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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