在另一个窗口文本框中重复wpf Datagrid数据 [英] Repeat wpf Datagrid data in another window text boxes

查看:78
本文介绍了在另一个窗口文本框中重复wpf Datagrid数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想在另一个窗口的文本框中重复datagrid数据。数据已保存在sql数据库中,我想在新窗口中根据单击一个数据网格行检索它们。

我该怎么办?谢谢。



Hi every one,
I want to repeat datagrid data in textboxes of another window. Data has been saved in sql databse and i want to retrieve them on the new window based on click on one datagrid row.
how can i do? thanks.

<DataGrid Margin="0,23,0,0" AutoGenerateColumns="False" EnableRowVirtualization="True"
                 ItemsSource="{Binding}" Name="grdPeople" VerticalContentAlignment="Center"
                 IsReadOnly="True" DataContext="{Binding}">

           <DataGrid.Columns>
               <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" Width="Auto" ></DataGridTextColumn>
               <DataGridTextColumn Binding="{Binding Path=Job}" Header="Job" Width="Auto"></DataGridTextColumn>
               <DataGridTemplateColumn Header="Picture" Width="45" >
                   <DataGridTemplateColumn.CellTemplate >
                       <DataTemplate >
                           <Image Source="{Binding Path=Picture}" Width="30" Height="30" Stretch="Uniform">
                           </Image>
                       </DataTemplate>
                   </DataGridTemplateColumn.CellTemplate>
               </DataGridTemplateColumn>
           </DataGrid.Columns>
       </DataGrid>

推荐答案

定义并实现一个包含事件对象的接口。用部分窗口类的另一部分写一个单独的文件非常方便,那么你不需要在这个类的不同部分提到接口。在其中一个窗口中实现它。例如,网格视图所在的窗口可以引入,事件 DataGridRawSelected 。根据您自己的事件参数类(从 System.EventArgs 类派生)创建它,以传递您需要在不同窗口中显示的所有数据。



由于第一个窗口的实例现在可以由接口实例表示,而其他所有内容都隐藏在用户之外,您可以将此接口引用传递给第二个窗口。它应该在接口类型的类中有一个字段。将此字段分配给第一个窗口。现在,您的第二个窗口有一个接口类型的字段,接口引用指向第一个窗口,但只访问接口成员。在第二个窗口的代码中,为事件添加处理程序,该事件是接口的成员。事件处理程序将接收事件参数,该参数携带与所选原始数据相关的所有信息,但仅包含需要在第二个窗口中显示的信息。使用对现在可访问的文本框的引用,因为处理程序是第二个窗口的方法。在这些文本框中显示此信息。



另一种方法是:第二个窗口的实现和接口;这个接口属性应该表示要在文本框中显示的文本,但是以某种抽象的形式(只是使用你的语义名称来表示这些字符串的含义),在第一个窗口中传递它,它应该使用接口引用来填充文本数据。



但是,第一种方法提供了更好的隔离和更好的UI抽象。接口仅用于处理事件。你可以考虑任何组合方法。



我这里没有说新的东西;您应该能够创建和实现接口,交换接口实例而不是实现类/结构的引用,定义事件实例并将事件处理程序添加到接口实例的调用列表中。所有这些杂务都是任何UI开发的必备条件。我只建议整齐的包装和强大的代码设计。



-SA
Define and implement some interface with an event object in it. It''s very convenient to write a separate file with another part of the partial window class, then you would not need to mention the interface in different parts of this class. Implement it in one of the window. For example, a window where you grid view is can introduce and event DataGridRawSelected. Create it based on your own event arguments class (derived from System.EventArgs class) to pass all data you need to show in a different window.

As the instance of the first window can now be represented by an interface instance, with everything else hidden from the user, you can pass this interface reference to the second window. It should have a field in its class of the interface type. Assign this field to the first window. Now your second window have a field of the interface type, and the interface reference points to the first window, but only interface members are accessed. In the code of the second window, add a handler to the event, which is a member of the interface. An event handler will receive event arguments which carry all the information related to the selected raw, but only the information needed to be presented in a second window. Use the reference to the text boxes, which are now accessible, because the handler is the method of a second window. Show this information in these text boxes.

Another way around is possible: implement and interface for the second window; this interface properties should represent texts to be shown in the text boxes, but in some abstract form (just use your semantic names for the meaning of those strings), pass it in the first window, which should use the interface reference to fill in the text data.

However, first approach provides better isolation and better abstracts out the UI. An interface is used only for handling an event. You can consider any combination approaches.

I did not say anything new here; you should be able to create and implement interfaces, exchange interface instances instead of references to implementing classes/structures, define event instances and add event handlers to the invocation lists of interface instances. All of these chores is a must for any UI development. I only suggested a neatly packages and robust design of the code.

—SA


这篇关于在另一个窗口文本框中重复wpf Datagrid数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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