【问题标题】:C# Strange WPF Combobox BehaviorC# 奇怪的 WPF 组合框行为
【发布时间】:2011-01-13 07:22:27
【问题描述】:

我有简单的窗口。 这是我单击 ComboBox 时发生的情况: 列表显示在屏幕的左上角,而不是在 Combobox 下方。

XAML:

<Window x:Class="WpfPortOfTestingCamera.VideoSettings"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Video Settings" WindowStartupLocation="CenterOwner" ResizeMode="NoResize" ShowInTaskbar="False" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" SizeToContent="WidthAndHeight" d:DesignHeight="167">
    <StackPanel Name="stackPanel1" VerticalAlignment="Top" HorizontalAlignment="Center">
        <GroupBox Header="Settings" Name="groupBox1">
            <Grid Name="grid1" VerticalAlignment="Center" HorizontalAlignment="Center">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80*" />
                    <ColumnDefinition Width="175*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Label Content="Resolution:" Height="28" Name="label1" Margin="0" HorizontalAlignment="Left" VerticalAlignment="Center" />
                <Label Content="Framerate:" Height="28" HorizontalAlignment="Left" Margin="0" Name="label2" VerticalAlignment="Center" Grid.Row="1" />
                <ComboBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="0" Name="comboBox1" VerticalAlignment="Center" Width="150" SelectionChanged="comboBox1_SelectionChanged" />
                <ComboBox Height="23" HorizontalAlignment="Left" Margin="0" Name="comboBox2" VerticalAlignment="Center" Width="150" Grid.Column="1" Grid.Row="1" SelectionChanged="comboBox2_SelectionChanged" />
            </Grid>
        </GroupBox>
        <Label Name="labelSelectedSize" Content="Size @ FPS" />
        <Button Name="button1" Content="Apply" Click="button1_Click" />
    </StackPanel>
</Window>

【问题讨论】:

  • 对我来说没问题。你在后面的代码中有什么?
  • @Andrei Pana 当我从另一个加载事件中打开这个窗口时发生了。我真的需要解决这个问题。
  • 不要直接在Loaded事件中打开它,只需在Dispatcher上排队另一条消息即可打开它。

标签: c# .net wpf xaml combobox


【解决方案1】:

不要直接在 Loaded 事件中打开它,只需在 Dispatcher 上排队另一条消息即可打开它。

【讨论】:

    【解决方案2】:

    我恰好遇到了这个问题,刚刚在WPF ComboBox DropDown part appears in the wrong place 上发布了一个对我有用的示例。感兴趣的读者可以去那里仔细阅读我的评论,但这里是 sn-p(注意:WindoBaseLoadedHandler 是 XAML 中指定的“Loaded=”处理程序):

    protected void WindowBaseLoadedHandler(object sender, RoutedEventArgs e)
    {
    

    ...删除了不必要的代码行...

        if (DataContext != null)
        {
            Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
            {
                this.IsEnabled = false;
    
                LoginDlg loginDlg = new LoginDlg();
                loginDlg.ShowDialog();
    
                if (!loginDlg.Success)
                {
                    /*-----------------------------------
                     * Log on failed -- terminate app...
                     *----------------------------------*/
                    ...termination logic removed...
                }
    
                this.IsEnabled = true;
            }));
        }
    

    【讨论】:

      猜你喜欢
      • 2010-12-23
      • 1970-01-01
      • 2012-12-25
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 2015-01-14
      • 2014-11-26
      • 1970-01-01
      相关资源
      最近更新 更多