ComboboxItem扩展

时间:2013-11-21 18:55:25

标签: wpf vb.net combobox

这似乎很容易找到/实现,但我没有运气。

我需要一个简单的组合框,其中一个项目在鼠标悬停在其上时向右扩展以获得更多选项。我尝试添加第二个组合框作为第一个组合的孩子,但我无法使用它向右开启。

我没有和孩子的组合框结婚,并且会采取任何好的建议。

下面的代码给了我一个错误:'设置属性'System.Windows.Controls.ItemsControl.ItemsSource'引发了一个异常。行号'154'和行位置'123',其中行154是

<ComboBoxItem Tag="163" Style="{StaticResource blackComboBoxItem}">£ - Pound</ComboBoxItem>

这是代码:

<ComboBox Name="SettingsCmbx" Foreground="White" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" SelectedIndex="0"  Width="80" Style="{StaticResource blackGradientComboBox}" Margin="3">
    <ComboBox.ItemsSource>
        <CompositeCollection>
            <ComboBoxItem Name="Options" IsEnabled="False" Style="{StaticResource blackComboBoxItem}" FontSize="12" Foreground="White">Options</ComboBoxItem>
            <ComboBoxItem Style="{StaticResource blackComboBoxItem}">Guidelines</ComboBoxItem>
            <ComboBoxItem Style="{StaticResource blackComboBoxItem}">Copy Investigation</ComboBoxItem>
            <ComboBox Name="CurrencySelectCmbx" Foreground="White" VerticalContentAlignment="Center"  HorizontalContentAlignment="Center" SelectedIndex="0" Width="80" Style="{StaticResource blackGradientComboBox}">
                <Popup x:Name="PART_Popup" Placement="Right"></Popup>
                <ComboBox.ItemsSource>
                    <CompositeCollection>
                        <ComboBoxItem Tag="36" Style="{StaticResource blackComboBoxItem}">Currency</ComboBoxItem>
                        <ComboBoxItem Tag="36" Style="{StaticResource blackComboBoxItem}">$ - Dollar</ComboBoxItem>
                        <ComboBoxItem Tag="163" Style="{StaticResource blackComboBoxItem}">£ - Pound</ComboBoxItem>
                    </CompositeCollection>
                </ComboBox.ItemsSource>
            </ComboBox>

了解后端是如何实现的(不完整):

Private Sub SettingsCmbx_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles SettingsCmbx.SelectionChanged
    Dim selection As String = SettingsCmbx.SelectedItem.content.ToString()
    If selection = "Options" Then Return

    If selection = "Guidelines" Then
        Dim answer As MsgBoxResult = MsgBox("Navigate to NDIA website to view 32 Guidelines?", MsgBoxStyle.OkCancel)
        If answer = MsgBoxResult.Ok Then
            Process.Start("http://www.ndia.org/Divisions/Divisions/Procurement/PMSC/Pages/PMSCCommitteeDocuments.aspx")
        End If
    ElseIf selection = "About" Then
        Dim helpwindow As New HelpWindow
        helpwindow.Show()
    ElseIf selection = "Copy Investigation" Then
        Dim _investigationcopymappingwindow As New InvestigationCopyMappingWindow
        _investigationcopymappingwindow.Show()
    ElseIf selection = "Currency" Then
        'WHERE THE CHILD SELECTION CODE SHOULD THEORETICALLY GO
    End If

    SettingsCmbx.SelectedIndex() = 0
End Sub

1 个答案:

答案 0 :(得分:1)

您可以使用ComboBox中的菜单系统:

            <ComboBox Grid.Column="2" Height="25">
                <MenuItem x:Name="MenuItem1" Header="Testing" Background="White" BorderThickness="0" MouseEnter="MenuItem_MouseEnter">
                <MenuItem Header="IfThisWorks" Background="White" BorderThickness="0"/>
            </MenuItem>

鼠标悬停时用此命令打开菜单:

    private void MenuItem_MouseEnter(object sender, MouseEventArgs e)
    {
        MenuItem1.IsSubmenuOpen = true;
    }