当“子可见性”设置为“加载时折叠”时,WPF窗口无法正确显示SizeToContent

时间:2017-03-03 21:07:25

标签: c# wpf

我在WPF窗口中遇到一个奇怪的问题,我打电话给.ShowDialog()

我有SizeToContent="Height",它有点有效。最初大小为300(WPF Windows的默认值),当我显示对话框时,它会缩小到其子项的大小。但是,其中一个子项会根据对话框的属性(故意)消失,并且窗口不会调整大小以反映这一点。它只是留下了一个黑色的空间。

Window with black space

真正奇怪的是,如果在Window_Loaded处理程序中,我调用MessageBox.Show("foo");,它会正确调整大小。

Message box

Window resized correctly

我猜这是Dispatcher或渲染器的一些怪癖,或者我不理解的一些竞争条件。

有关如何解决此问题的任何想法?这是一个非常小的侧面项目,所以让它重绘或调整大小,而不是“正确行事”完全没问题。

如果重要,请输入以下代码:

AnxietyTracker.xaml

<Window x:Class="Wages.AnxietyTracker"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:xkct="http://schemas.xceed.com/wpf/xaml/toolkit"
        xmlns:local="clr-namespace:Wages"
        mc:Ignorable="d"
        Title="Track Anxiety" Width="300" Loaded="Window_Loaded"
        SizeToContent="Height">
    <StackPanel>
        <DockPanel Margin="0 4 0 0" Visibility="{Binding Path=InitialVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}">
            <TextBlock Margin="4 0">Initial anixety</TextBlock>
            <xkct:IntegerUpDown Name="iudInitial" Maximum="100" Minimum="0" Margin="4 0" Increment="5" Value="0" KeyDown="iudInitial_KeyDown" />
        </DockPanel>
        <Grid Visibility="{Binding Path=IntervalVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <TextBlock Margin="4 0" Grid.Row="0" Grid.Column="0">Peak anxiety</TextBlock>
            <xkct:IntegerUpDown Name="iudPeak" Maximum="100" Minimum="0" Margin="4 0" Grid.Row="0" Grid.Column="1" Increment="5" Value="0"
                                KeyDown="iudPeak_KeyDown" />
            <TextBlock Margin="4 0" Grid.Row="1" Grid.Column="0">Current anxiety</TextBlock>
            <xkct:IntegerUpDown Name="iudCurrent" Maximum="100" Minimum="0" Margin="4 0"  Grid.Row="1" Grid.Column="1" Increment="5" Value="0"
                                KeyDown="iudInitial_KeyDown" />
        </Grid>
        <Button Name="btnSubmit" HorizontalAlignment="Right" Margin="4" Click="btnSubmit_Click">Submit</Button>
    </StackPanel>
</Window>

AnxietyTracker.xaml.cs

// using ...
namespace Wages
{
    /// <summary>
    /// Interaction logic for AnxietyTracker.xaml
    /// </summary>
    public partial class AnxietyTracker : Window
    {
        public enum TrackState
        {
            Initial,
            Interval,
            End
        }

        private TrackState m_trackState;

        public AnxietyTracker(TrackState state)
        {
            InitializeComponent();
            m_trackState = state;
        }

        public int CurrentAnxiety { get; set; }
        public int PeakAnxiety { get; set; }

        public Visibility InitialVisibility
        {
            get
            {
                if (m_trackState == TrackState.Initial)
                {
                    return Visibility.Visible;
                }

                return Visibility.Collapsed;
            }
        }

        public Visibility IntervalVisibility
        {
            get
            {
                if (m_trackState != TrackState.Initial)
                {
                    return Visibility.Visible;
                }

                return Visibility.Collapsed;
            }
        }

        // handlers for irrelevant stuff ...

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Window w = sender as Window;
            w.Activate();

            if (CurrentAnxiety != 0)
            {
                iudCurrent.Value = CurrentAnxiety;
            }

            if (PeakAnxiety != 0)
            {
                iudPeak.Value = PeakAnxiety;
            }

            if (iudInitial.Visibility == Visibility.Visible)
            {
                iudInitial.Focus();
            }
            else
            {
                iudPeak.Focus();
            }

            // System.Windows.MessageBox.Show("foo");
        }
    }
}

0 个答案:

没有答案