Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf PropertyGrid的自定义组合框控件_Wpf_Vb.net_Combobox - Fatal编程技术网

Wpf PropertyGrid的自定义组合框控件

Wpf PropertyGrid的自定义组合框控件,wpf,vb.net,combobox,Wpf,Vb.net,Combobox,我需要一些帮助为属性网格创建自定义组合框编辑器。如果数据库连接发生更改,组合框的itemsource将动态更改。主要问题是我不知道如何访问所选项目。 我尝试通过一个SelectionChanged事件来实现这一点,结果成功了。但是在我选择了一个项目之后,如果数据库连接发生了变化,itemsource就不再发生变化。 如果有人对我如何做到这一点有了其他想法,我也非常感谢任何帮助和信息 该编辑器称为SingleSelectEditor,在属性网格中使用,如下所示: Private _idNameLi

我需要一些帮助为属性网格创建自定义组合框编辑器。如果数据库连接发生更改,组合框的itemsource将动态更改。主要问题是我不知道如何访问所选项目。 我尝试通过一个
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
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
至少ViewModel看起来是这样的:

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
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