WPF在页面重新加载时为ComboBox设置SelectedValue

时间:2015-08-12 21:52:50

标签: wpf vb.net combobox

我有一个在页面初始化时创建的ComboBox

string screenName = HttpContext.Current.User.Identity.GetScreenName();

并添加到工具栏

它填充在加载事件

 Dim CategoryCombo As New CustomControl.ComboCBx
        With CategoryCombo
            .Name = "MaintTypes_CatCombo"
        End With
        RegisterControl(MaintenanceTypes_Grid, CategoryCombo)
        vToolBar.Items.Add(CategoryCombo)
        vToolBar.Items.Add(TS_Separator)

如果用户导航到另一个页面并返回所选的值,则返回到所选的索引0.我可以在离开页面之前获取最后选择的值但是在页面重新加载时找不到设置.SelectedValue的方法

数据来自

Dim CatCombo As CustomControl.ComboCBx = MaintenanceTypes_Grid.FindName("MaintTypes_CatCombo")
 With CatCombo
            .IsNewRecord = False
            .Width = 200
            .ItemsSource = ReturnCategories.DefaultView
            .SelectedValuePath = "ID"
            .DisplayMemberPath = "Name"
            .SelectedIndex = 0

        End With

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:0)

找到一个解决方法 - 从DataTable返回索引并使用该

设置ComboBox的.SelectedIndex

如果有人在返回页面之前删除了所选项目,请先检查该行是否存在

 Dim vIndex As Integer = 0
        If Not CurrentCategory = 0 Then
            Dim vRow As DataRow = CatDT.Select("ID = '" & CurrentCategory & "'").FirstOrDefault()
            If Not vRow Is Nothing Then
                vIndex = CatDT.Rows.IndexOf(vRow)
            End If
        End If
        With CatCombo
            .IsNewRecord = False
            .Width = 200
            .ItemsSource = ReturnCategories.DefaultView
            .SelectedValuePath = "ID"
            .DisplayMemberPath = "Name"
            .SelectedIndex = vIndex
        End With