WPF会隐藏一行,但空格不会扫出

时间:2017-05-15 13:28:24

标签: c# wpf

我有一个带网格的wpf窗口。我想隐藏行,我使用此代码,如其他stackoverflow线程所示:

            buttonsGrid.Visibility = Visibility.Collapsed; or buttonsGrid.Height = 0;
            dayTimePicker.Visibility = Visibility.Collapsed; or dayTimePicker.Height = 0;

将可见性设置为折叠或高度为0会产生相同的效果,但我希望这些行占用的空白区域扫描...我希望整个窗口被索引的行0占用。我能做到吗?

这是我的窗口:

<Window x:Class="PerformanceVideoRec.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    xmlns:DTControl="clr-namespace:DayTimePickControl"
    Title="{StaticResource ID_TITLE}" Height="600" Width="900" Loaded="Window_Loaded"> <!--Closing="Window_Closing"> -->

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid Grid.Row="1" x:Name="ControlPart" IsEnabled="False">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="6*"/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="0" x:Name="LeftGrid">
            <Grid.RowDefinitions>
                <RowDefinition Height="55*"/>
                <RowDefinition Height="5*"/>
                <RowDefinition Height="5*"/>
                <RowDefinition Height="20*"/>
            </Grid.RowDefinitions>
            <WindowsFormsHost Grid.Row="0" Margin="5" Background="Gray">
                <wf:PictureBox x:Name="playWindow"></wf:PictureBox>
            </WindowsFormsHost>
            <UniformGrid Grid.Row="1">
                <Button x:Name="btnShowHideControls" Content="Nascondi Controlli" Click="btnShowHideControls_Click"/>
            </UniformGrid>
            <UniformGrid Columns="6" Grid.Row="2" x:Name="buttonsGrid">
                <Button x:Name="btnPause" Margin="5" Width="50" Content="{StaticResource ID_PAUSE}" Click="btnPause_Click"></Button>
                <Button x:Name="btnPlay" Margin="5" Width="50" Content="{StaticResource ID_PLAY}" Click="btnPlay_Click"  Visibility="Collapsed"/>
                <Button x:Name="btnStop" Margin="5" Width="50" Content="{StaticResource ID_STOP}" Click="btnStop_Click"></Button>
                <Button x:Name="btnSlow" Margin="5" Width="50" Content="{StaticResource ID_SLOW}" Click="btnSlow_Click"></Button>
                <TextBox x:Name="tbSpeed" IsEnabled="False" Width="50" Height="25" TextAlignment="Center" VerticalContentAlignment="Center" Text="1X" />
                <Button x:Name="btnFast" Margin="5" Width="50" Content="{StaticResource ID_FAST}" Click="btnFast_Click"></Button>
                <Button x:Name="btnNormal" Margin="5" Width="50" Content="{StaticResource ID_NORMAL}" Click="btnNormal_Click"></Button>
            </UniformGrid>
            <DTControl:DayTimePick x:Name="dayTimePicker" Grid.Row="3" Width="550" Height="100" Grid.RowSpan="2" OnTimeClick="dayTimePicker_OnTimeClick"></DTControl:DayTimePick>
        </Grid>
        <Frame x:Name="PlayBackFrame" Grid.Column="1" Background="AliceBlue" ></Frame>

    </Grid>
</Grid>

3 个答案:

答案 0 :(得分:2)

您可以使用Data Binding并使用转换器绑定要折叠的行内控件的属性Visibility

我通常在使用数据绑定时使用NuGet包Mvvm Lightdocumentation来获得一些功能。 (在这种情况下RelayCommandRaisePropertyChanged实施)

一个非常基本的例子:

部分观点。在这里,您可以注意到按钮的Visibility属性与IsButton1Visible的属性ViewModel之间的绑定。当然boolVisibility的类型不同,所以我必须使用IValueConverter创建映射:

 <Grid>

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <Button Grid.Row="0" Content="1" Visibility="{Binding IsButton1Visible, Converter={StaticResource BooleanToVisibilityConverter}}" />
    <Button Grid.Row="1" Content="2" />
    <Button Grid.Row="2" Content="3" />
    <Button Grid.Row="3" Content="4" />
    <Button Grid.Row="4" Content="Toggle button 1 visibility" Command="{Binding ToggleButton1Visibility}" />

</Grid>

后面代码中的窗口构造函数(您也可以通过ViewModel属性将DataContext直接绑定到View),用于将ViewViewModel相关联public MainWindow() { InitializeComponent(); this.DataContext = new MyWindowViewModel(); }

true

要将Visibility.Visible转换为public class BooleanToVisibilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => (bool)value ? Visibility.Visible : Visibility.Collapsed; public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } 的转换器:

App.xaml

Application.ResourceView部分,用于使<Application.Resources> <local:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /> </Application.Resources> “看到”转换器:

RaisePropertyChanged

ViewModel。 set IsButton1Visible View方法的View非常重要,因为它会通知public class MyWindowViewModel : ViewModelBase { private bool _isButton1Visibile = true; public bool IsButton1Visible { get => _isButton1Visibile; set { if (_isButton1Visibile == value) return; _isButton1Visibile = value; RaisePropertyChanged(nameof(IsButton1Visible)); } } RelayCommand _toggleButton1Visbility; public RelayCommand ToggleButton1Visibility { get { return _toggleButton1Visbility ?? (_toggleButton1Visbility = new RelayCommand( () => { IsButton1Visible = !IsButton1Visible; })); } } } 该属性已更改,以便<?php $siteOwnersEmail = 'myemail@yahoo.com'; if($_POST) { $name = trim(stripslashes($_POST['contactName'])); $email = trim(stripslashes($_POST['contactEmail'])); $subject = trim(stripslashes($_POST['contactSubject'])); $contact_message = trim(stripslashes($_POST['contactMessage'])); // Check Name if (strlen($name) < 2) { $error['name'] = "Please enter your name."; } // Check Email if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) { $error['email'] = "Please enter a valid email address."; } // Check Message if (strlen($contact_message) < 15) { $error['message'] = "Please enter your message. It should have at least 15 characters."; } // Subject if ($subject == '') { $subject = "Contact Form Submission"; } // Set Message $message .= "Email from: " . $name . "<br />"; $message .= "Email address: " . $email . "<br />"; $message .= "Message: <br />"; $message .= $contact_message; $message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />"; // Set From: header $from = $name . " <" . $email . ">"; // Email Headers $headers = "From: " . $from . "\r\n"; $headers .= "Reply-To: ". $email . "\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; if (!$error) { ini_set("sendmail_from", $siteOwnersEmail); // for windows server $mail = mail($siteOwnersEmail, $subject, $message, $headers); if ($mail) { echo "OK"; } else { echo "Something went wrong. Please try again."; } } # end if - no validation error else { $response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null; $response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null; $response .= (isset($error['message'])) ? $error['message'] . "<br />" : null; echo $response; } # end if - there was a validation error } ?> 可以自行刷新:< / p>

Traceback (most recent call last):
  File "/version1/analyze.py", line 447, in <module>
    cv_results = model_selection.cross_val_score(model, X_train, Y_train,cv=kfold, scoring=scoring) 

 File "/usr/lib64/python2.7/site-packages/sklearn/model_selection/_validation.py", line 140, in cross_val_score
    for train, test in cv_iter)

fac = 1. / (n_samples - n_classes)

ZeroDivisionError: float division by zero

答案 1 :(得分:2)

MVVM方式:

  • 为要折叠的UI部分创建单独的视图模型。我们称之为PlayViewModel,
  • 为它制作一个DataTemplate,
  • 将它作为属性公开(例如,让我们称之为Play,不要忘记提出属性更改事件)在视图模型中,它是整个视图的DataContext

  • 使用ContentPresenter,ContentPresenter.Content绑定到Play属性

  • 在网格中显示它
  • 你通过归零Play属性来隐藏,你可以通过恢复它来显示它。

您可以免费获得可测试性。

答案 2 :(得分:0)

LeftGrid.RowDefinitions[2].Height = new GridLength(0);
LeftGrid.RowDefinitions[3].Height = new GridLength(0);

如果你想恢复make this

   LeftGrid.RowDefinitions[2].Height = new GridLength(5, GridUnitType.Star);
    LeftGrid.RowDefinitions[3].Height = new GridLength(20, GridUnitType.Star);