从下拉列表中选择值后,如何从ComboBox中失去焦点?

时间:2015-08-17 19:43:28

标签: c# xaml silverlight combobox

在组合框中选择值后,我试图阻止在组合框中使用方向键。组合框的功能是,当选择一个值时,它不能再被点击,但在下拉关闭后,方向键可以选择已经使用的值。我想失去焦点'组合框或防止方向键被使用。

XAML

<Grid Grid.Row="1" Margin="20 10 20 0">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="40" />
        <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
    <Grid.Resources>
        <Style TargetType="ComboBoxItem">
            <Setter Property="FontFamily" Value="Segoe UI"/>
            <Setter Property="Background" Value="#242424"/>
            <Setter Property="Foreground" Value="#FFFFFF" />
            <Setter Property="MinHeight" Value="20"/>
            <Setter Property="FontSize" Value="14"/>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="IsEnabled" Value="{Binding IsAvailable}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBoxItem">
                        <Grid Background="{TemplateBinding Background}">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="HighlightVisual">
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="Visible"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled">
                                       <Storyboard>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Content">
                                               <DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0.33"/>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="SelectionStates">
                                    <VisualState x:Name="Unselected"/>
                                    <VisualState x:Name="Selected">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="HighlightVisual2">
                                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="Visible"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="FocusStates">
                                    <VisualState x:Name="Focused"/>
                                    <VisualState x:Name="Unfocused"/>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Grid>
                                <Border x:Name="HighlightVisual" Width="{TemplateBinding Width}" BorderBrush="#88FFFFFF" Margin="1" BorderThickness="1" Background="#33FFFFFF" CornerRadius="1" Visibility="Collapsed"/>
                                <Border x:Name="HighlightVisual2" Width="{TemplateBinding Width}" Background="#33FFFFFF" Visibility="Collapsed"/>
                                <ContentPresenter x:Name="Content" ContentTemplate="{TemplateBinding ContentTemplate}" Margin="5 0 5 0" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                            </Grid>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>

    <TextBlock Text="{Binding Path=StringLibrary.LM_Priority, Source={StaticResource Strings}}" FontFamily="Segoe UI" Foreground="#FFFFFF" FontSize="12" VerticalAlignment="Center" />

    <ComboBox Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Width="200" Margin="10 0 0 0" ItemsSource="{Binding Priorities}" SelectedValuePath="PriorityNumber" SelectedValue="{Binding SelectedPriority, Mode=TwoWay}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid>
                        <TextBlock Text="{Binding PriorityNumber}" FontFamily="Segoe UI Semibold" Foreground="#FFFFFF" FontSize="12" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="4 0 0 0" />

                        <TextBlock Text="{Binding CurrentList}" FontFamily="Segoe UI" FontStyle="Italic" MaxWidth="150" Foreground="#FFFFFF" FontSize="12" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0 0 4 0" />
                    </Grid>
                </Grid>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

2 个答案:

答案 0 :(得分:0)

感谢您的评论。 Nighthawk441的建议可以正常工作,但是Helix 88让我想起只使用快捷方式来失去ComboBox的焦点。我刚创建了一个'虚拟'TextBox,并在DropDown关闭后将焦点设置为。我添加了

DropDownClosed="ComboBox_DropDownClosed"

到ComboBox以下是我为感兴趣的人所做的更改。再次感谢。

<!--DROPDOWN-->
<ComboBox Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Width="200" Margin="10 0 0 0" ItemsSource="{Binding Priorities}" SelectedValuePath="PriorityNumber" SelectedValue="{Binding SelectedPriority, Mode=TwoWay}" DropDownClosed="ComboBox_DropDownClosed">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid>
                    <!--PRIORITY NUMBER-->
                    <TextBlock Text="{Binding PriorityNumber}" FontFamily="Segoe UI Semibold" Foreground="#FFFFFF" FontSize="12" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="4 0 0 0" />

                    <!--CURRENT LIST-->
                    <TextBlock Text="{Binding CurrentList}" FontFamily="Segoe UI" FontStyle="Italic" MaxWidth="150" Foreground="#FFFFFF" FontSize="12" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0 0 4 0" />
                </Grid>
            </Grid>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

'虚拟'文本框

<!--used to lose focus when dropdown is closed-->
    <TextBox x:Name="dummyTB" Width="0" Height="0" Opacity="0" VerticalAlignment="Top" HorizontalAlignment="Right" />

代码背后

private void ComboBox_DropDownClosed(object sender, EventArgs e)
    {
        dummyTB.Focus();
    }

答案 1 :(得分:0)

当使用MouseWheel在新关闭的ComboBox上滚动时,会发生同样的问题,无论是在独立Combobox中,还是在我通过DataGridTemplateColumn添加到DataGrid的Combobox中,就像这样:

    <DataGrid x:Name="dgvFieldsMapping" Grid.Row="1" ItemsSource="{Binding}">
        <DataGrid.Columns>
            ...
            <DataGridTemplateColumn Width="*" Header="Destination Field" >
                <DataGridTemplateColumn.CellTemplate >
                    <DataTemplate >
                        <ComboBox ItemsSource="{Binding Source={StaticResource CustomerDbFields}}" SelectedValue="{Binding destinationField, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ></ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            ...
        </DataGrid.Columns>
    </DataGrid>

因此,无论何时关闭DropDown,鼠标滚轮仍将滚动该Combobox的Items并修改Selection。

我最终将XAML修改为如下形式:

    <DataGrid x:Name="dgvFieldsMapping" Grid.Row="1" ItemsSource="{Binding}">
        <DataGrid.Resources>
            <Style x:Key="dgvComboBox_Loaded" TargetType="ComboBox">
                <EventSetter Event="Loaded" Handler="dgvCombobox_Loaded" />
            </Style>
        </DataGrid.Resources>
        <DataGrid.Columns>
            ...
            <DataGridTemplateColumn Width="*" Header="Destination Field" >
                <DataGridTemplateColumn.CellTemplate >
                    <DataTemplate >
                        <ComboBox Style="{StaticResource dgvComboBox_Loaded}" ItemsSource="{Binding Source={StaticResource CustomerDbFields}}" SelectedValue="{Binding destinationField, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ></ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            ...
        </DataGrid.Columns>
    </DataGrid>

并将这些行添加到代码隐藏中

        public void dgvCombobox_Loaded(Object sender, RoutedEventArgs e)
        {
            ((ComboBox)sender).DropDownClosed -= ComboBox_OnDropDownClosed;
            ((ComboBox)sender).DropDownClosed += new System.EventHandler(ComboBox_OnDropDownClosed);
        }

        void ComboBox_OnDropDownClosed(object sender, System.EventArgs e)
        {
            dgvFieldsMapping.Focus();
        }

通过这种方式,我只需在关闭其对应的DropDown之后将Focus从ComboBox移到外部DataGrid,并且不需要添加任何虚拟FrameWorkElement