在组合框(WPF)的选择更改事件上填充文本框中的值吗? [英] Populating values in text box on the selection changed event of Combo Box(WPF)?

查看:69
本文介绍了在组合框(WPF)的选择更改事件上填充文本框中的值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一个带组合框和多个文本框的wpf应用程序.

在窗口加载事件中,组合框将填充员工ID.当我选择任何ID时,以下文本框应显示所选"ID"的名称,城市,邮政编码,国家/地区.
我确实写了下面的代码来填充文本框中的值.

Hi there,

I have a wpf application which has combo box and multiple text boxes.

On the window load event the combo box is filled with the Employee IDs. When I select any ID then the below text boxes should display the name, city, zip, country of the selected "Id".
I did wrote the below code for filling in the values in text boxes.

Private Sub ViewMode(ByVal GUID As String)
        Try

            Dim viewDs As New DataSet()
            Dim Query As String
            Query = "Select * from tblSystemEmployeeInfo Where ID = '" & GUID.Trim & "'"
            viewDs = GetResultDataset(Query, True)

            Dim dv As DataView = New DataView(viewDs.Tables(0))
            Dim Notify As String = String.Empty
            If dv.Count > 0 Then

                If Not IsDBNull(dv(0)("ID")) Then
                    txtHWGUID.Text = dv(0)("ID")
                Else
                    txtHWGUID.Text = String.Empty
                End If

               

                If Not IsDBNull(dv(0)("StreetName"))     Then
                    txtHWStName.Text = dv(0)("StreetName")
                Else
                    txtHWStName.Text = String.Empty
                End If
            End If
        Catch ex As Exception
            MsgBox("No Values Found")
        End Try
    End Sub


并在组合框的选择更改事件上调用了以下函数,如下所示:


And called the above function on Selection Changed Event of Combo Box as below:

Private Sub ID CB_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles IDCB.SelectionChanged
        StreetCB.IsEnabled = False
ViewMode(IDCB.SelectedItem)



尽管它命中了ViewMode函数,但它从未在我的文本框中填充任何值.

我想念任何东西吗?
有什么办法可以实现?

任何帮助将不胜感激.

非常感谢!



Though it hits the ViewMode Function but it never populates any values in my text boxes.

Am I missing any thing in it?
Is there any way I could achieve?

Any help will be much appreciated.

Thank you so much!

推荐答案



在WPF中,如果您不使用绑定基础结构,则会丢失很多内容.

在以下示例中,我试图模仿您的情况,并使用绑定来做到这一点,

唯一的缺点是客户数据存在于客户端上,而不是按需从服务器中获取的数据

1.确保创建一个代表您的模型的类,
Hi,

in WPF if you don''t use the binding infrastructure then you are missing a lot.

In the following sample I tried to mimic your case, and use the binding to do exactly that,

the only drawback is that the customers data are present on the client, they are not fetched from the server on demand

1. make sure to create a class that represents your model,
public class Customer
{
   public int Id { get; set; }
   public string Name { get; set; }
   public int Age { get; set; }
}


2.将您的类实例打包到一个列表中(在我的示例中为IEnumerable客户)
3.在加载事件时将该集合赋予组合框的items source属性,


2. pack your class instances in a list (in my sample an IEnumerable of customers)
3. give that collection to the items source property of the combobox at the load event,

Loaded += (sender, args) => {
   var customers = from i in Enumerable.Range(1, 10)
                   select
                       new Customer() {
                         Id = i,
                         Name = "Customer " + i.ToString(),
                         Age = i + 20};
   comboBox1.ItemsSource = customers;
};


这样,您将有一个由该列表填充的组合框,每个组合框项都将打印您创建的类型名称->默认行为是调用ToString,这里有2个解决方案,
首先->返回您的自定义类并覆盖ToString以仅返回ID,
第二->通过将组合框的属性设置为display
来使用组合框的DisplayMemberPath


by this you have a combobox that is populated by that list, each combobox item will print the type name you created --> the default behavior is to call ToString, you have 2 solutions here,
first --> go back to your custom class and override ToString to just return the Id,
second --> make use of the DisplayMemberPath of the combobox by giving it the property to display

<combobox height="23" displaymemberpath="Id" horizontalalignment="Left" .../>



现在我们在UI中有了数据,我们将使用绑定将所有内容粘合在一起.



now we have the data in the UI, we will use the binding to glue everything together.

<textbox height="23" text="{Binding ElementName=comboBox1, Path=SelectedItem.Name}" .../>
<textbox height="23" text="{Binding ElementName=comboBox1, Path=SelectedItem.Age}" .../>


现在,每当组合框选中的项目更改时,两个文本框都将被更新.
希望此解决方案对您有所帮助.


Now whenever the combo box selected item change, both text boxes will be updated.
hope this solution is helpful to you.


实际上,WPF使用DataBinding而不是直接从后面的代码中设置属性.尝试一下,开始时它看起来会很复杂,但是请习惯它!
当用户在组合框中更改选择时,将触发两个SelectionChanged事件(至少在Windows Forms应用程序中,我不确定WPF):第一个事件是删除上一个选择并且未选择任何项(即IDCB). SelectedItem是Nothing!),以及选择新项目时的秒数.因此,请确保仅对正确"事件做出反应(即,当IDCB.SelectedItem为Nothing时不执行任何操作).并在ViewMode函数中的某个位置设置一个断点,并逐步通过断点以查找更多详细信息.
Actually WPF uses DataBinding instead of setting properties directly from code behind. Try that, it will look very complicted when you start, but get used to it!
When the user changes the selection in a combobox, two SelectionChanged events are fired (at least in a Windows Forms application, I am not sure with WPF): the first event when the previous selection was removed and no item is selected (i.e. IDCB.SelectedItem is Nothing!), and the second when the new item was selected. So make sure to react on the "correct" event only (i.e. do nothing when IDCB.SelectedItem is Nothing). And also set a break point somewhere in the ViewMode function and step thru it to find out more details.


这篇关于在组合框(WPF)的选择更改事件上填充文本框中的值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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