在WPF中增加窗口大小时出现黑条

时间:2017-11-23 09:28:19

标签: c# wpf checkbox window

我有一个带有两个复选框的小程序。

窗口高度为120.当选中其中一个时,窗口高度应增加到180.如果两者都被检查,它应该增加到220.如果取消选中它们,它们应该减少到180表示1个复选框,120表示没有检查。

我使它平滑,这意味着我计算windowHeightOld和windowHeightNew差异,将它除以30并将其增加30个小步骤,使其看起来很平滑。

问题在于窗口尺寸减小看起来很大,增加的区域会增加黑条几毫秒,然后再次消失。可悲的是我无法截屏,因为它有多短。

我不知道它来自哪里以及如何解决。

这里的代码是从窗口高度的增加/减少中删除的:

private Session getSession() throws JSchException
    {
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        JSch jsch = new JSch();
        Session session;
        session = jsch.getSession(VENDOR1_USERID, VENDOR1_SERVER, VENDOR1_PORT);
        session.setPassword(VENDOR1_PASSWORD);
        session.setConfig(config);
        session.connect();
        return session;
    }

对于长长的代码行不好,但我认为有必要深入了解。

基本上它会检查这两个是否以不同的组合进行检查。并根据它增加或减少窗口高度。并且因为我也有文本框(这就是我这样做的原因。当选中复选框时我想显示相应的文本框)我根据复选框设置了可见或不可见。

所以基本上它的两个方法(单击Checkbox1并单击Checkbox2)并且两者都构建相同。单击一个时,它会检查组合,相应地设置窗口高度,并显示或设置相应的文本框不可见。

0复选框已选中:

enter image description here

选中1复选框:

enter image description here

检查了2个复选框:

enter image description here 我希望我解释得很好。

1 个答案:

答案 0 :(得分:0)

XAML:

<Window x:Class=.........
        DataContext="{StaticResource MainViewModel}"
        Name="MyWindow"
        >

    <Window.Triggers >
        <EventTrigger  RoutedEvent="CheckBox.Unchecked" >
            <BeginStoryboard  >
                <Storyboard   >
                    <DoubleAnimation  Storyboard.TargetName="MyWindow" Storyboard.TargetProperty = "(Window.Height)" Duration="0:0:0.5" To="{Binding WindowHeight}"  RepeatBehavior="1x" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger  RoutedEvent="CheckBox.Checked"  >
            <BeginStoryboard  >
                <Storyboard   >
                    <DoubleAnimation  Storyboard.TargetName="MyWindow" Storyboard.TargetProperty = "(Window.Height)" Duration="0:0:0.5" To="{Binding WindowHeight}"  RepeatBehavior="1x" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Window.Triggers>

    <Grid Row="0" >
        ............
        <CheckBox   Grid.Column="4" 
                    Content="UpadatePack" 
                    IsChecked="{Binding PackegeCheck, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" 
                    Name="CheckUpadatePack" />
        <CheckBox   Grid.Column="5" 
                    Content="Setup" 
                    IsChecked="{Binding SetupCheck, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" 
                    Name="CheckSetup"  />
        ...... 
    </Grid>
    <Grid Row="5" Visibility="{Binding SetupDestinationVisibility, Converter={StaticResource BoolToVisibilityConverter}}" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="120"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Label Content="Setup Destination" Margin="5" Grid.Column="0"/>
        <TextBox  Margin="5" Grid.Column="1"/>
    </Grid>
    <Grid Row="6" Visibility="{Binding PackageDestinationVisibility, Converter={StaticResource BoolToVisibilityConverter}}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="120"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Label Content="Package Destination" Margin="5" Grid.Column="0"/>
        <TextBox  Margin="5" Grid.Column="1"/>
    </Grid>

</Window>

MainViewModel:

public class MainViewModel : ViewModelBase
{

    private bool _sixtyFourBitCheck;
    private bool _packegeCheck;
    private bool _setupCheck;
    private bool _ftpUploadCheck;
    public int _windowHeight;
    public int _origSize = 200;


    public bool SixtyFourBitCheck
    {
        get => _sixtyFourBitCheck;
        set
        {
            if (value == _sixtyFourBitCheck) return;
            _sixtyFourBitCheck = value;
            OnPropertyChanged();
        }
    }

    public bool PackegeCheck
    {
        get => _packegeCheck;
        set
        {
            if (value == _packegeCheck) return;
            _packegeCheck = value;
            OnPropertyChanged();
            OnPropertyChanged(nameof(PackageDestinationVisibility));
            ChangeWindowsHeght();
        }
    }

    public bool SetupCheck
    {
        get => _setupCheck;
        set
        {
            if (value == _setupCheck) return;
            _setupCheck = value;
            OnPropertyChanged();
            OnPropertyChanged(nameof(SetupDestinationVisibility));
            ChangeWindowsHeght();
        }
    }

    public bool FtpUploadCheck
    {
        get => _ftpUploadCheck;
        set
        {
            if (value == _ftpUploadCheck) return;
            _ftpUploadCheck = value;
            OnPropertyChanged();
        }
    }

    public bool SetupDestinationVisibility => SetupCheck;

    public bool PackageDestinationVisibility => PackegeCheck;

    public int WindowHeight
    {
        get => _windowHeight;
        set
        {
            if (value == _windowHeight) return;
            _windowHeight = value;
            OnPropertyChanged();
        }
    }

    public MainViewModel()
    {
        WindowHeight = _origSize;
    }

    private void ChangeWindowsHeght()
    {
        WindowHeight = _origSize;
        if (PackageDestinationVisibility)
            WindowHeight += 35;
        if (SetupDestinationVisibility)
            WindowHeight += 35;
        OnPropertyChanged(nameof(WindowHeight));
    }
}