2

我对 wpf 还很陌生,所以请多多包涵。

我的问题是,我有 4 个图形控件放置在另一个上方,显示在弹出窗口中。最初默认显示所有 4 个,但在显示窗口后,用户可以选择取消选中功能区控件中的复选框以关闭一个或多个图形。我使用 UniformGrid 是因为剩余的图形会自动调整大小以占用剩余空间。

我现在需要添加一个 RowSplitter,以便用户可以控制每个可见图形的高度。顶部图形的顶部应锚定到窗口的顶部,底部图形的底部应锚定到窗口的底部。当用户在 2 个图形之间滑动 RowSplitter 时,它应该会影响它们的高度。

我做了一个简单的测试项目来尝试和工作。

<Window.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <StackPanel Background="LightGray"  Grid.Row="0" Orientation="Horizontal">
            <CheckBox Content="Graph 1" Name="Graph1Vis" IsChecked="True" HorizontalAlignment="Left"/>
            <CheckBox Content="Graph 2" Name="Graph2Vis" IsChecked="True" HorizontalAlignment="Left"/>
            <CheckBox Content="Graph 3" Name="Graph3Vis" IsChecked="True" HorizontalAlignment="Left"/>
            <CheckBox Content="Graph 4" Name="Graph4Vis" IsChecked="True" HorizontalAlignment="Left"/>
        </StackPanel>
        <UniformGrid Grid.Row="1" Height="Auto"  Columns="1">
            <Label Content="Graph 1" Background="Azure" Grid.Row="0" 
                   Visibility="{Binding IsChecked, ElementName=Graph1Vis, Converter={StaticResource BooleanToVisibilityConverter}}"/>
            <GridSplitter Grid.Row="1" Width="Auto" Height="8" Background="DarkSlateBlue"
                        HorizontalAlignment="Stretch" VerticalAlignment="Bottom"/>
        <Label Content="Graph 2" Background="Lavender" Grid.Row="2" 
               Visibility="{Binding IsChecked, ElementName=Graph2Vis, Converter={StaticResource BooleanToVisibilityConverter}}"/>
            <GridSplitter Grid.Row="3" Width="Auto" Height="8" Background="DarkSlateBlue"
                        HorizontalAlignment="Stretch" VerticalAlignment="Top"/>
        <Label Content="Graph 3" Background="Moccasin" Grid.Row="4"
               Visibility="{Binding IsChecked, ElementName=Graph3Vis, Converter={StaticResource BooleanToVisibilityConverter}}"/>
            <GridSplitter Grid.Row="5" Width="Auto" Height="8" Background="DarkSlateBlue"
                        HorizontalAlignment="Stretch" VerticalAlignment="Top"/>
        <Label Content="Graph 4" Background="Beige" Grid.Row="6"
               Visibility="{Binding IsChecked, ElementName=Graph4Vis, Converter={StaticResource BooleanToVisibilityConverter}}"/>
    </UniformGrid>
    </Grid>

任何帮助将不胜感激。

4

3 回答 3

2

我的想法是实际添加/删除网格中的行。这需要对其他元素进行一些调整,但我认为可以按照您的目标使用网格/网格分割器。该解决方案可以保持非常通用。一个好处是行高被记住了,所以如果你再次显示一个图表,它会显示在它之前的高度。此外,当删除一行时,其他图表将按比例占用释放的空间,这似乎是正确的行为。

我没有使用每个 gridsplitter 的一行,我只是在每行的底部放置一个(最后一行除外)。xaml 部分:

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel x:Name="panelVisibilities" Background="LightGray"  Grid.Row="0" Orientation="Horizontal">
            <CheckBox Content="Graph 1" x:Name="Graph1Vis" IsChecked="True" HorizontalAlignment="Left" Click="GraphVis_Click"/>
            <CheckBox Content="Graph 2" x:Name="Graph2Vis" IsChecked="True" HorizontalAlignment="Left" Click="GraphVis_Click"/>
            <CheckBox Content="Graph 3" x:Name="Graph3Vis" IsChecked="True" HorizontalAlignment="Left" Click="GraphVis_Click"/>
            <CheckBox Content="Graph 4" x:Name="Graph4Vis" IsChecked="True" HorizontalAlignment="Left" Click="GraphVis_Click"/>
        </StackPanel>
        <Grid Grid.Row="1" VerticalAlignment="Stretch" x:Name="graphGrid" >
            <Grid.RowDefinitions>
                <RowDefinition Height="*" MinHeight="20"/>
                <RowDefinition Height="*" MinHeight="20"/>
                <RowDefinition Height="*" MinHeight="20"/>
                <RowDefinition Height="*" MinHeight="20"/>
            </Grid.RowDefinitions> 
            <Label Grid.Row="0" Content="Graph 1" Background="Azure"  Visibility="{Binding IsChecked, ElementName=Graph1Vis, Converter={StaticResource BooleanToVisibilityConverter}}"/>
            <GridSplitter Grid.Row="0" ResizeBehavior="CurrentAndNext" VerticalAlignment="Bottom" Height="2" Background="DarkSlateBlue" HorizontalAlignment="Stretch"  Visibility="{Binding IsChecked, ElementName=Graph1Vis, Converter={StaticResource BooleanToVisibilityConverter}}" />
            <Label Grid.Row="1" Content="Graph 2" Background="Lavender"  Visibility="{Binding IsChecked, ElementName=Graph2Vis, Converter={StaticResource BooleanToVisibilityConverter}}"/>
            <GridSplitter Grid.Row="1" ResizeBehavior="CurrentAndNext" VerticalAlignment="Bottom" Height="2" Background="DarkSlateBlue" HorizontalAlignment="Stretch"  Visibility="{Binding IsChecked, ElementName=Graph2Vis, Converter={StaticResource BooleanToVisibilityConverter}}" />
            <Label Grid.Row="2" Content="Graph 3" Background="Moccasin" Visibility="{Binding IsChecked, ElementName=Graph3Vis, Converter={StaticResource BooleanToVisibilityConverter}}"/>
            <GridSplitter Grid.Row="2" ResizeBehavior="CurrentAndNext" VerticalAlignment="Bottom" Height="2" Background="DarkSlateBlue" HorizontalAlignment="Stretch" Visibility="{Binding IsChecked, ElementName=Graph3Vis, Converter={StaticResource BooleanToVisibilityConverter}}" />
            <Label Grid.Row="3" Content="Graph 4" Background="Beige" Visibility="{Binding IsChecked, ElementName=Graph4Vis, Converter={StaticResource BooleanToVisibilityConverter}}"/>
        </Grid>
    </Grid>
</UserControl>

后面的代码:

public partial class UserControl1 : UserControl
{
    private List<RowDefinition> _rows; // rows in graphGrid
    private List<CheckBox> _visibilityCheckboxes; // checkboxes that determine graph visibilities
    private Dictionary<int, List<UIElement>> _elementsByRow = new Dictionary<int, List<UIElement>>(); // map of elements per row


    public UserControl1()
    {
        InitializeComponent();

        _visibilityCheckboxes = this.panelVisibilities.Children.OfType<CheckBox>().ToList();
        _rows = this.graphGrid.RowDefinitions.ToList();

        // create map of elements per row
        var elements = this.graphGrid.Children.Cast<UIElement>();
        for (int i = 0; i < _rows.Count; i++)
        {
            _elementsByRow.Add(i, elements.Where(e => Grid.GetRow(e) == i).ToList());
        }

        // check counts (optional)
        if (_rows.Count != _visibilityCheckboxes.Count)
            System.Diagnostics.Debug.WriteLine("Graph count does not match checkbox count.");


    }

    /// <summary>
    /// Handles the visibility checkboxes click.
    /// Adds/removes rows from the graphGrid, and adjusts the other elements accordingly.
    /// </summary>
    private void GraphVis_Click(object sender, RoutedEventArgs e)
    {
        CheckBox chbx = sender as CheckBox;

        if (chbx != null && _visibilityCheckboxes.Contains(chbx))
        {
            int actualIndex = GetActualIndex(chbx); // row index at which the change occurs
            int originalIndex = _visibilityCheckboxes.IndexOf(chbx); // original index of the inserted row
            bool isChecked = (bool)chbx.IsChecked;

            RepositionControls(actualIndex, isChecked); 

            if (isChecked)
            {
                this.graphGrid.RowDefinitions.Insert(actualIndex, _rows[originalIndex]); // add row
                foreach (var element in _elementsByRow[originalIndex])
                    Grid.SetRow(element, actualIndex); // set element's grid.row to inserted row number
            }
            else
                this.graphGrid.RowDefinitions.RemoveAt(actualIndex); // remove row
        }

    }

    /// <summary>
    /// Determines at which row number the change needs to occur.
    /// E.g. when checking chbx3, but only chbx1 is already checked (chbx2 is not), 
    /// the index of change (zero based) is 1 and not 2.
    /// </summary>
    /// <param name="chbx">Checkbox that was just changed</param>
    private int GetActualIndex(CheckBox chbx)
    {
        int i = 0;
        foreach (var c in _visibilityCheckboxes)
        {
            if (c == chbx) break;
            if ((bool)c.IsChecked) i++;
        }
        return i;
    }

    /// <summary>
    /// Moves elements to the appropriate grid row
    /// </summary>
    /// <param name="startIndex">Row number, at which the change occurs</param>
    /// <param name="isChecked">True if adding row, false when removing row</param>
    private void RepositionControls(int startIndex, bool isChecked)
    {
        if (!isChecked) startIndex++; // e.g. remove at row1 --> elements in row2, row3.. need to be adjusted

        // get all elements that need row adjustment (IsMeasureValid is false for non-displayed elements - is there a better property to use?)
        var elementsToMove = this.graphGrid.Children.Cast<UIElement>().Where(e => Grid.GetRow(e) >= startIndex && e.IsMeasureValid);

        foreach (var element in elementsToMove)
        {
            // increase or decrease grid.row property
            Grid.SetRow(element, Grid.GetRow(element) + (isChecked ? 1 : -1)); 
        }
    }
}

一个可能的改进是总是从最底部的图表中删除网格分割器,我猜这不会太难弄清楚。

于 2013-02-27T11:47:53.677 回答
1

想法是这样的:构建一个有 4 行的网格,每行有 1 个图表。将 3 个矩形作为拆分器按钮添加到网格中,并将它们的 Grid.RowSpan 设置为 4。

MainWindow.xaml:

<Window x:Class="testwpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        MouseMove="Window_MouseMove_1" MouseUp="Window_MouseUp_1">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Name="row1" Height="40"/>
            <RowDefinition Name="row2" Height="40"/>
            <RowDefinition Name="row3" Height="40"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Rectangle Grid.Row="0" Fill="Green"/>
        <Rectangle Grid.Row="1" Fill="Blue"/>
        <Rectangle Grid.Row="2" Fill="Red"/>
        <Rectangle Grid.Row="3" Fill="Yellow"/>
        <Rectangle Fill="#5000" VerticalAlignment="Top" Height="10" Name="splitter1" MouseDown="splitter_MouseDown_1" Grid.RowSpan="4" Margin="0,40,0,0"/>
        <Rectangle Fill="#5000" VerticalAlignment="Top" Height="10" Name="splitter2" MouseDown="splitter_MouseDown_1" Grid.RowSpan="4" Margin="0,80,0,0"/>
        <Rectangle Fill="#5000" VerticalAlignment="Top" Height="10" Name="splitter3" MouseDown="splitter_MouseDown_1" Grid.RowSpan="4" Margin="0,120,0,0"/>
    </Grid>
</Window>

MainWindow.xaml.cs:

    FrameworkElement dragginSplitter = null;
    private void splitter_MouseDown_1(object sender, MouseButtonEventArgs e)
    {
        dragginSplitter = sender as FrameworkElement;
    }
    private void Window_MouseMove_1(object sender, MouseEventArgs e)
    {
        if (dragginSplitter != null)
            dragginSplitter.Margin = new Thickness(0, e.GetPosition(this).Y, 0, 0);
        if (splitter2.Margin.Top < splitter1.Margin.Top + 10) splitter2.Margin = new Thickness(0, splitter1.Margin.Top + 10, 0, 0);
        if (splitter3.Margin.Top < splitter2.Margin.Top + 10) splitter3.Margin = new Thickness(0, splitter2.Margin.Top + 10, 0, 0);
        row1.Height = new GridLength(splitter1.Margin.Top);
        row2.Height = new GridLength(splitter2.Margin.Top - splitter1.Margin.Top);
        row3.Height = new GridLength(splitter3.Margin.Top - splitter2.Margin.Top);
    }
    private void Window_MouseUp_1(object sender, MouseButtonEventArgs e)
    {
        dragginSplitter = null;
    }

Window_MouseMove_1 的重要说明:

  1. 每当拖动拆分器时,draggingSplitter 的上边距等于鼠标的 Y 位置。
  2. 拆分器不应相互通过,因此每当拖动拆分器时,我们都会检查它们的上边距。
  3. 每当拖动拆分器时,根据拆分器的上边距计算网格行的高度(不是网格的内容)。

编辑:


我修改了代码并添加了各种条件以满足隐藏/显示的需要。

<Window x:Class="testwpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        MouseMove="Window_MouseMove_1" MouseUp="Window_MouseUp_1">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Name="row1" Height="40"/>
            <RowDefinition Name="row2" Height="40"/>
            <RowDefinition Name="row3" Height="40"/>
            <RowDefinition Name="row4" Height="*"/>
        </Grid.RowDefinitions>
        <Rectangle Grid.Row="0" Fill="Green"/>
        <Rectangle Grid.Row="1" Fill="Blue"/>
        <Rectangle Grid.Row="2" Fill="Red"/>
        <Rectangle Grid.Row="3" Fill="Yellow"/>
        <Rectangle Fill="#5000" VerticalAlignment="Top" Height="10" Name="splitter1" MouseDown="splitter_MouseDown_1" Grid.RowSpan="4" Margin="0,40,0,0"/>
        <Rectangle Fill="#5000" VerticalAlignment="Top" Height="10" Name="splitter2" MouseDown="splitter_MouseDown_1" Grid.RowSpan="4" Margin="0,80,0,0"/>
        <Rectangle Fill="#5000" VerticalAlignment="Top" Height="10" Name="splitter3" MouseDown="splitter_MouseDown_1" Grid.RowSpan="4" Margin="0,120,0,0"/>
        <WrapPanel Grid.RowSpan="4">
            <CheckBox Name="check1" Content="graph1" IsChecked="True" Checked="CheckBox_Checked_1" Unchecked="CheckBox_Checked_1"/>
            <CheckBox Name="check2" Content="graph2" IsChecked="True" Checked="CheckBox_Checked_1" Unchecked="CheckBox_Checked_1"/>
            <CheckBox Name="check3" Content="graph3" IsChecked="True" Checked="CheckBox_Checked_1" Unchecked="CheckBox_Checked_1"/>
            <CheckBox Name="check4" Content="graph4" IsChecked="True" Checked="CheckBox_Checked_1" Unchecked="CheckBox_Checked_1"/>
        </WrapPanel>
    </Grid>
</Window>

xaml 更改:添加了 4 个复选框并命名了第 4 行。

FrameworkElement dragginSplitter = null;
private void splitter_MouseDown_1(object sender, MouseButtonEventArgs e)
{
    dragginSplitter = sender as FrameworkElement;
}

private void Window_MouseMove_1(object sender, MouseEventArgs e)
{
    if (dragginSplitter != null)
    {
        dragginSplitter.Margin = new Thickness(0, e.GetPosition(this).Y, 0, 0);
        updateSplitters();
    }
}

private void updateSplitters()
{
    if (splitter2.Margin.Top < splitter1.Margin.Top + 10) splitter2.Margin = new Thickness(0, splitter1.Margin.Top + 10, 0, 0);
    if (splitter3.Margin.Top < splitter2.Margin.Top + 10) splitter3.Margin = new Thickness(0, splitter2.Margin.Top + 10, 0, 0);

    if (check1.IsChecked.Value)
    {
        if (!check2.IsChecked.Value && !check3.IsChecked.Value && !check4.IsChecked.Value)
            row1.Height = new GridLength(1, GridUnitType.Star);
        else 
            row1.Height = new GridLength(splitter1.Margin.Top);
    }
    else
        row1.Height = new GridLength(0);

    if (check2.IsChecked.Value)
    {
        if (!check3.IsChecked.Value && !check4.IsChecked.Value)
            row2.Height = new GridLength(1, GridUnitType.Star);
        else if(check1.IsChecked.Value)
            row2.Height = new GridLength(splitter2.Margin.Top - splitter1.Margin.Top);
        else
            row2.Height = new GridLength(splitter2.Margin.Top);
    }
    else
        row2.Height = new GridLength(0);

    if (check3.IsChecked.Value)
    {
        if (!check4.IsChecked.Value)
            row3.Height = new GridLength(1, GridUnitType.Star);
        else if (check2.IsChecked.Value)
            row3.Height = new GridLength(splitter3.Margin.Top - splitter2.Margin.Top);
        else if (check1.IsChecked.Value)
            row3.Height = new GridLength(splitter3.Margin.Top - splitter1.Margin.Top);
        else
            row3.Height = new GridLength(splitter3.Margin.Top);
    }
    else
        row3.Height = new GridLength(0);

    row4.Height = check4.IsChecked.Value ? new GridLength(1, GridUnitType.Star) : new GridLength(0);
}


private void Window_MouseUp_1(object sender, MouseButtonEventArgs e)
{
    dragginSplitter = null;
}

private void CheckBox_Checked_1(object sender, RoutedEventArgs e)
{
    if (check4 == null) return;//for when not yet completely loaded

    if (!check1.IsChecked.Value)
        splitter1.Visibility = System.Windows.Visibility.Collapsed;
    if (!check2.IsChecked.Value)
        splitter2.Visibility = System.Windows.Visibility.Collapsed;
    if (!check3.IsChecked.Value)
        splitter3.Visibility = System.Windows.Visibility.Collapsed;
    if (!check4.IsChecked.Value)
    {
        splitter3.Visibility = System.Windows.Visibility.Collapsed;
        if (!check3.IsChecked.Value)
            splitter2.Visibility = System.Windows.Visibility.Collapsed;
        if (!check2.IsChecked.Value)
            splitter1.Visibility = System.Windows.Visibility.Collapsed;
    }

    if (check1.IsChecked.Value && (check2.IsChecked.Value || check3.IsChecked.Value || check4.IsChecked.Value))
        splitter1.Visibility = System.Windows.Visibility.Visible;
    if (check2.IsChecked.Value && (check3.IsChecked.Value || check4.IsChecked.Value))
        splitter2.Visibility = System.Windows.Visibility.Visible;
    if (check3.IsChecked.Value && check4.IsChecked.Value)
        splitter3.Visibility = System.Windows.Visibility.Visible;

    updateSplitters();
}

cs的变化:

  1. updateSplitters 函数被替换为之前用于更新拆分器边距的逻辑。
  2. 检查或未选中任何复选框时,调用分离器更改和更新更新的可见性。
  3. updateSplitters 完成所有其余的工作。没什么好说的,除了最后一个可见行总是有 Star 的 GridUnitType 来填充网格的剩余空间。

我发现的唯一问题是,当窗口调整大小或将上面的行之一调整为较大的高度值时,网格的下部内容可能会从屏幕上掉下来。

于 2013-02-21T23:18:59.210 回答
0

以下是我所做的更改...

MainWindow.xaml:

在本节中,请确保添加以下内容:

在此处输入图像描述

<Window.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>

<Grid>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <StackPanel Background="LightGray" Grid.Row="0" Orientation="Horizontal">
            <CheckBox Content="Graph 1" x:Name="Graph1Vis" IsChecked="True" HorizontalAlignment="Left"/>
            <CheckBox Content="Graph 2" x:Name="Graph2Vis" IsChecked="True" HorizontalAlignment="Left"/>
            <CheckBox Content="Graph 3" x:Name="Graph3Vis" IsChecked="True" HorizontalAlignment="Left"/>
            <CheckBox Content="Graph 4" x:Name="Graph4Vis" IsChecked="True" HorizontalAlignment="Left"/>
        </StackPanel>
            
        <Grid Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="{Binding Mode=TwoWay, Path=IsChecked, ElementName=Graph1Vis, Converter={local:CheckedToLengthConverter}}"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="{Binding Mode=TwoWay, Path=IsChecked, ElementName=Graph2Vis, Converter={local:CheckedToLengthConverter}}"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="{Binding Mode=TwoWay, Path=IsChecked, ElementName=Graph3Vis, Converter={local:CheckedToLengthConverter}}"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="{Binding Mode=TwoWay, Path=IsChecked, ElementName=Graph4Vis, Converter={local:CheckedToLengthConverter}}"></RowDefinition>
            </Grid.RowDefinitions>
                
            <Label Content="Graph 1" Background="Azure" Grid.Row="0" Visibility="{Binding IsChecked, ElementName=Graph1Vis, Converter={StaticResource BooleanToVisibilityConverter}}"/>
            <GridSplitter Grid.Row="1" Width="Auto" Height="8" Background="DarkSlateBlue" HorizontalAlignment="Stretch" VerticalAlignment="Center" Visibility="{Binding IsChecked, ElementName=Graph1Vis, Converter={StaticResource BooleanToVisibilityConverter}}" />
            <Label Content="Graph 2" Background="Lavender" Grid.Row="2" Visibility="{Binding IsChecked, ElementName=Graph2Vis, Converter={StaticResource BooleanToVisibilityConverter}}"/>
            <GridSplitter Grid.Row="3" Height="8" Background="DarkSlateBlue" HorizontalAlignment="Stretch" VerticalAlignment="Center" Visibility="{Binding IsChecked, ElementName=Graph2Vis, Converter={StaticResource BooleanToVisibilityConverter}}"/>
            <Label Content="Graph 3" Background="Moccasin" Grid.Row="4" Visibility="{Binding IsChecked, ElementName=Graph3Vis, Converter={StaticResource BooleanToVisibilityConverter}}"/>
            <GridSplitter Grid.Row="5" Width="Auto" Height="8" Background="DarkSlateBlue" HorizontalAlignment="Stretch" VerticalAlignment="Center" Visibility="{Binding IsChecked, ElementName=Graph4Vis, Converter={StaticResource BooleanToVisibilityConverter}}"/>
            <Label Content="Graph 4" Background="Beige" Grid.Row="6" Visibility="{Binding IsChecked, ElementName=Graph4Vis, Converter={StaticResource BooleanToVisibilityConverter}}"/>
        </Grid>
    </Grid>
</Grid>

主窗口.xaml.cs

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    {
public class CheckedToLengthConverter : System.Windows.Markup.MarkupExtension, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return System.Convert.ToBoolean(value) ? new GridLength(1, GridUnitType.Star) : new GridLength(0, GridUnitType.Pixel);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Binding.DoNothing;
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
//etc...

我希望这是您正在寻找的,如果您有任何问题/疑虑,请告诉我!


此外,由于您刚刚开始,我将为您提供一些有用的建议。

姓名

使用 WPF 时,某些元素没有属性Name,这就是为什么我建议x:Name在命名元素时使用。


网格分割器

每当使用GridSplitter时,请确保提供以下内容:

  • 垂直对齐
  • 水平对齐
  • 宽度和/或高度

这将确保GridSplitter遗嘱有效。

在您的示例中,每个GridSplitter都有不同的VerticalAlignment值,这将导致结果不一致。我建议使用VerticalAlignment="Center".


网格/统一网格

我建议使用Gridover UniformGrid。可以说是最强大的布局容器,并且比(很少使用)Grid提供更多的自由。UniformGrid

于 2013-02-26T19:06:59.143 回答