当另一个图形为Visibility.Collapsed时,一个图形会占用空间

本文关键字:图形 占用空间 一个 另一个 Visibility Collapsed | 更新日期: 2023-09-27 18:00:19

这是C#WPF和xaml。我有主窗口,我有两个图形共享这个窗口。它们是垂直排列的。它们与主窗口的宽度相同。但是,当第二个图形折叠时,我希望第一个图形填充整个窗口(除了窗口顶部的一些边距),当两个图形都可见时,我也希望它们共享高度(每个图形的高度=(窗口高度)/2)。

以下是我在xaml中尝试的,但没有成功:

<Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <d3:ChartPlotter  Grid.Row="0" Name ="timeDomainPlotter" >
                </d3:ChartPlotter>
                <d3:ChartPlotter  Grid.Row="1" Name ="freqDomainPlotter" >
                </d3:ChartPlotter>
            </Grid>

当第二个窗口为Visibility.Collapsed时,第一个窗口不会占用第二个窗的空间。我该怎么做?谢谢

更新:弹出窗口中有两个图形的转换器代码:

 public class VisibilityToHeightConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                GridLength height = new GridLength();
                var visiblity = (Visibility)value;
                switch (visiblity)
                {
                    case Visibility.Collapsed:
                        height = new GridLength(0, GridUnitType.Auto);
                        break;
                    case Visibility.Visible:
                        height = new GridLength(1, GridUnitType.Star);
                        break;
                    case Visibility.Hidden:
                        height = new GridLength(0, GridUnitType.Auto);
                        break;
                }
                return height;
            }
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
        /// <summary>
        /// Interaction logic for SignalStatsDisplay.xaml
        /// </summary>
        public partial class SignalStatsDisplay : Window
        {

xaml用于弹出窗口:

<Window x:Class="FileWatcherCaller.View.SignalStatsDisplay"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"
   xmlns:local="clr-namespace:FileWatcherCaller.View"
    Title="Real Time Signal Display" Height="409" Width="1200">
    <Window.Resources>
        <local:VisibilityToHeightConverter x:Key="VisToHeightConv"/>
    </Window.Resources>
    <Grid> 
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Horizontal">
                <CheckBox Height="16" HorizontalAlignment="Left" Name="pixVal" VerticalAlignment="Top" Width="120" Checked="checkBox1_Checked">Pixel Value</CheckBox>
                <CheckBox Height="16" HorizontalAlignment="Left" Name="roiMean" VerticalAlignment="Top" Width="120">ROI Mean</CheckBox>
                <CheckBox Height="16" HorizontalAlignment="Left" Name="roiStd" VerticalAlignment="Top" Width="120" Checked="roiStd_Checked">ROI Std</CheckBox>
            </StackPanel>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="{Binding ElementName=timeDomainPlotter, Path=Visibility, Converter={StaticResource VisToHeightConv}}" Name="RowOne" />
                    <RowDefinition Height="{Binding ElementName=freqDomainPlotter, Path=Visibility, Converter={StaticResource VisToHeightConv}}" Name="RowTwo" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <d3:ChartPlotter  Grid.Row="0" Name ="timeDomainPlotter" >
                </d3:ChartPlotter>
                <d3:ChartPlotter  Grid.Row="1" Name ="freqDomainPlotter" >
                </d3:ChartPlotter>
            </Grid>
        </StackPanel>
    </Grid>
</Window>

在主窗口中,如何初始化两个图的可见性:

public void StartWatch()
        {
            if (_fileWatcher != null)
            {
                _fileWatcher.Dispose();
                _fileWatcher = null;
            }
            if (InitWatcher())
            {
                this._fileWatcher.Start();
                this.ButtonStart.IsEnabled = false;
                this.ButtonStop.IsEnabled = true;
            }
             _signalDisplay = new SignalStatsDisplay();
            if (_signalDisplay.Visibility != Visibility.Visible)
            {
                _signalDisplay.Show();
                _signalDisplay.timeDomainPlotter.Visibility = Visibility.Visible;
                _signalDisplay.freqDomainPlotter.Visibility = Visibility.Collapsed;
            }

        }

对于Kevin的愤怒,我有弹出窗口的xaml:

<Window x:Class="FileWatcherCaller.View.SignalStatsDisplay"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"
    Title="Real Time Signal Display" Height="409" Width="1200">
    <Grid> 
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Horizontal">
                <CheckBox Height="16" HorizontalAlignment="Left" Name="pixVal" VerticalAlignment="Top" Width="120" Checked="checkBox1_Checked">Pixel Value</CheckBox>
                <CheckBox Height="16" HorizontalAlignment="Left" Name="roiMean" VerticalAlignment="Top" Width="120">ROI Mean</CheckBox>
                <CheckBox Height="16" HorizontalAlignment="Left" Name="roiStd" VerticalAlignment="Top" Width="120" Checked="roiStd_Checked">ROI Std</CheckBox>
            </StackPanel>            
            <UniformGrid Columns="1">
                <d3:ChartPlotter Name ="timeDomainPlotter" >
                </d3:ChartPlotter>
                <d3:ChartPlotter Name ="freqDomainPlotter" >
                </d3:ChartPlotter>
            </UniformGrid>          
        </StackPanel>
    </Grid>
</Window>

但是,它并没有如预期的那样最大化顶部D3图。它仍然只占窗口的一半。我应该在后台代码中做些什么?

当另一个图形为Visibility.Collapsed时,一个图形会占用空间

<Window x:Class="FileWatcherCaller.View.SignalStatsDisplay"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"
    Title="Real Time Signal Display" Height="409" Width="1200">
    <Grid> 
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" Grid.Row="0">
            <CheckBox Height="16" HorizontalAlignment="Left" Name="pixVal" VerticalAlignment="Top" Width="120" Checked="checkBox1_Checked">Pixel Value</CheckBox>
            <CheckBox Height="16" HorizontalAlignment="Left" Name="roiMean" VerticalAlignment="Top" Width="120">ROI Mean</CheckBox>
            <CheckBox Height="16" HorizontalAlignment="Left" Name="roiStd" VerticalAlignment="Top" Width="120" Checked="roiStd_Checked">ROI Std</CheckBox>
        </StackPanel>            
        <UniformGrid Columns="1" Grid.Row="1">
            <d3:ChartPlotter Name ="timeDomainPlotter" >
            </d3:ChartPlotter>
            <d3:ChartPlotter Name ="freqDomainPlotter" >
            </d3:ChartPlotter>
        </UniformGrid>  
    </Grid>
</Window>

UniformGrid按照您想要的方式工作(只要您不希望用户调整两个部分的大小)

<UniformGrid Columns="1">
<TextBox Visibility="Collapsed">Hello</TextBox>
<TextBox Visibility="Visible">Goodbye</TextBox>
</UniformGrid>

对于更灵活的东西,我认为您将不得不编写一些代码。

以下是一个具有您所需行为的示例应用程序:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:VisibilityToHeightConverter x:Key="VisToHeightConv"/>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="{Binding ElementName=rctTop, Path=Visibility, Converter={StaticResource VisToHeightConv}}" Name="RowOne" />
            <RowDefinition Height="{Binding ElementName=rctBottom, Path=Visibility, Converter={StaticResource VisToHeightConv}}" Name="RowTwo" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Rectangle Fill="Black" Name="rctTop" Grid.Row="0"/>
        <Rectangle Fill="Red" Name="rctBottom" Grid.Row="1"/>
    </Grid>
</Window>

代码背后:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}
public class VisibilityToHeightConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        GridLength height = new GridLength();
        var visiblity = (Visibility)value;
        switch(visiblity)
        {
            case Visibility.Collapsed:
                height = new GridLength(0, GridUnitType.Auto);
                break;
            case Visibility.Visible:
                height = new GridLength(1, GridUnitType.Star);
                break;
            case Visibility.Hidden:
                height = new GridLength(0, GridUnitType.Auto);
                break;
        }
        return height;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

如果这段代码的任何部分不熟悉(值转换器、绑定),请告诉我们,我们将提供解释。