0

This is C# WPF and xaml. I have main window, and I have two graphs that share this window. They are vertically arranged. They both have same width as the main window. However, I want the first graph to fill the entire window (except for some margin on the top of the window) when the second one is collapsed, and I want them to share the height (each with Height = (Height of Window)/2 ) when both are visible.

Here is what I tried in xaml, not successful though:

<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>

The first window does not take over the second window's space when the second one is Visibility.Collapsed. How should I do this? Thanks.

Update: Converter code in pop up window where there are two graphs:

 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 for pop up window:

<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>

In main window, how the Visibility of two graphs are initialized:

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;
            }


        }

For Kevin's sulution, I have the xaml for the pop up window:

<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>

But still, it is not maximize the top D3 graph as expected. It is still takes only half of the window. Anything I should do in the behind code?

4

3 回答 3

2

UniformGrid以您正在寻找的方式工作(只要您不希望用户调整两个部分的大小)

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

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

于 2013-02-19T17:19:58.837 回答
2
<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>
于 2013-02-19T21:21:44.350 回答
1

这是一个具有您想要的行为的示例应用程序:

<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();
    }
}

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

于 2013-02-19T17:31:43.207 回答