我需要一些帮助为属性网格创建自定义组合框编辑器。如果更改数据库连接,组合框的项目源将动态更改。主要问题是我不知道如何访问所选项目。
我尝试使用SelectionChanged
事件执行此操作并且它有效。但是在我选择了一个项目之后,如果数据库连接发生了变化,则itemsource不再发生变化。
如果有人对我如何做到这一点有另一个想法,我也感谢任何帮助和信息。
编辑器名为SingleSelectEditor
,在属性网格中使用如下:
Private _idNameList As ObservableCollection(Of String) = New ObservableCollection(Of String)
<Category("2. ID Settings"), PropertyOrder(0)>
<Editor(GetType(SingleSelectEditor), GetType(SingleSelectEditor))>
Public Property IdNameList As ObservableCollection(Of String)
Get
Return _idNameList
End Get
Set(value As ObservableCollection(Of String))
_idNameList = value
End Set
End Property
现在用户控件如下所示:
<UserControl x:Class="SingleSelectEditor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="26" d:DesignWidth="200">
<Grid >
<ComboBox Name="SingleSelectComboBox" SelectedItem="{Binding SelectedString}" >
<ComboBox.ItemsSource>
<Binding Path="TestList" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
</ComboBox.ItemsSource>
</ComboBox>
</Grid>
背后的代码如下所示:
Public Class SingleSelectEditor
Implements ITypeEditor
Public Shared ReadOnly ValueProperty As DependencyProperty = _
DependencyProperty.Register("Value", GetType(ObservableCollection(Of String)), _
GetType(SingleSelectEditor), New FrameworkPropertyMetadata(Nothing, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault))
Public Property Value() As ObservableCollection(Of String)
Get
Return DirectCast(GetValue(ValueProperty), ObservableCollection(Of String))
End Get
Set(value As ObservableCollection(Of String))
SetValue(ValueProperty, value)
End Set
End Property
Public Function ResolveEditor(ByVal propertyItem As PropertyItem) As FrameworkElement Implements ITypeEditor.ResolveEditor
Dim binding = New Binding("Value")
binding.Source = propertyItem
binding.Mode = If(propertyItem.IsReadOnly, BindingMode.OneWay, BindingMode.TwoWay)
BindingOperations.SetBinding(Me, ValueProperty, binding)
Dim sSEVM As SingleSelectViewModel = New SingleSelectViewModel(Value)
SingleSelectComboBox.DataContext = sSEVM
Return Me
End Function
End Class
至少ViewModel看起来像这样:
Public Class SingleSelectViewModel
Inherits ViewModelBase
Private _testList As ObservableCollection(Of String) = New ObservableCollection(Of String)
Public Property TestList As ObservableCollection(Of String)
Get
Return _testList
End Get
Set(value As ObservableCollection(Of String))
_testList = value
End Set
End Property
Private _selectedString As String
Public Property SelectedString As String
Get
Return _selectedString
End Get
Set(value As String)
_selectedString = value
End Set
End Property
Public Sub New(ByVal tList As ObservableCollection(Of String))
TestList = tList
End Sub
End Class