扩展ComboBox导致无限循环的按钮

时间:2014-03-06 08:22:45

标签: c# wpf xaml

我的comboBox顶部有一个按钮,我收到了其他开发人员的元素,我无法彻底改变它。我需要发生的是当我将鼠标悬停在按钮上时,comboBox将打开,当我离开时它将关闭。当我单击按钮时,它将触发一个功能。问题是当我将鼠标悬停在按钮上时,comboBox会获得焦点,并且我陷入无限循环的开放关闭(可能还有其他错误)。但它不起作用。

<Grid Margin="10,0,10,0" Grid.Column="1">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="85*"/>
        <ColumnDefinition Width="15*"/>
    </Grid.ColumnDefinitions>
    <ComboBox Height="30" Name="Combo2"
              Style="{StaticResource CategoryComboBox}"
              SelectionChanged="ComboBox_SelectionChanged2"
              Grid.ColumnSpan="2"  Grid.Column="0">
        <ComboBoxItem Content="ComboBox Item 1 (Example)" />
        <ComboBoxItem Content="ComboBox Item 2 (Example)" />
        <ComboBoxItem Content="ComboBox Item 3 (Example)" />
        <ComboBoxItem Content="ComboBox Item 4 (Example)" />
    </ComboBox>
    <Button MouseEnter="MouseEnter2" MouseLeave="MouseLeave2" Name="Menu_Btn2"
            Click="GotoMenu" Style="{StaticResource CategoryButton}" Grid.Column="0"
            Content="Category Name 2" Margin="20,0,0,0" FontSize="16"
            FontFamily="pack://application:,,,/Fonts/#Univers LT Std 57 Cn" />
</Grid>

代码:

private void MouseEnter2(object sender, RoutedEventArgs e)
{
    Combo2.IsDropDownOpen = true;
}

private void MouseLeave2(object sender, RoutedEventArgs e)
{
    Combo2.IsDropDownOpen = false;
}

1 个答案:

答案 0 :(得分:1)

我的第一个想法是在您输入按钮时取消订阅MouseEnter2-Event。在MouseLeave2-Event中,您必须再次为MouseEnter2订阅。也许这对你有用

要取消订阅MouseEnter2-Event,您必须:

button.MouseEnter -= MouseEnter2;