视图框中的Wpf网格无法正常工作

时间:2017-07-07 21:51:24

标签: c# wpf grid resize viewbox

我正在尝试使用视图框来调整其中的网格大小。问题是在我在视图框中添加网格后,它会扭曲所有网格及其子网。 网格包含Listview,其中的项目具有自定义模板和样式,如下面的代码所示:

<Window x:Class="WpfApp3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp3"
    mc:Ignorable="d"
    SizeChanged="Window_SizeChanged"
    Title="MainWindow" Height="350" Width="525">

<Window.Resources>
    <Style TargetType="{x:Type ListViewItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <ContentPresenter/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="Focusable" Value="False"/>
    </Style>
    <DataTemplate x:Key="sectors">
        <Grid Background="White">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="169*"/>
                <ColumnDefinition Width="287*"/>
                <ColumnDefinition Width="30"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="{Binding Height}" />
            </Grid.RowDefinitions>
            <Image Grid.Row="0" Source="Images/drag.png" Width="20" Grid.Column="2" VerticalAlignment="Center" Cursor="SizeAll" ToolTip="Rivendos" Height="20" Margin="5,40"/>
            <Grid Grid.Row="0" Background="{Binding colorO}"  Grid.Column="0" Grid.ColumnSpan="2">
                <TextBlock Text="{Binding Name}" />
            </Grid>
            <Border Grid.Row="0" Grid.Column="2" BorderBrush="Gray" BorderThickness="0 0 0 8" Cursor="SizeNS"/>
        </Grid>
    </DataTemplate>
</Window.Resources>
<Grid Name="mainG">

</Grid>

MainWindow类:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Viewbox vb = new Viewbox();
            vb.Stretch = Stretch.Fill;
            Grid shelfGrid = new Grid();
            ListView shelf = new ListView();
            shelf.Background = new SolidColorBrush(Colors.Red);
            shelf.ItemTemplate = (DataTemplate)Resources["sectors"];
            ObservableCollection<Sector> sectors = new ObservableCollection<Sector>();
            sectors.Add(new Sector() { Id = 1, Width = 100, Height = 100, colorO = new SolidColorBrush(Colors.Red), Name = "Sector 1" });
            sectors.Add(new Sector() { Id = 2, Width = 100, Height = 200, colorO = new SolidColorBrush(Colors.Black), Name = "Sector 2" });
            shelf.ItemsSource = sectors;
            shelfGrid.Children.Add(shelf);
            shelfGrid.VerticalAlignment = VerticalAlignment.Stretch;
            mainG.Children.Add(shelfGrid);
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            double[] units = calculateUnits(350, 300);
            mainG.Width = units[0];
            mainG.Height = units[1];
        }

        public double[] calculateUnits(double widthR, double heightR)
        {
            double[] units = new double[3];
            double ratio = 0;
            double width = this.ActualWidth;
            double height = this.ActualHeight - 35;
            double maxShelf = Math.Max(widthR, heightR);
            double minPanel = Math.Min(width, height);
            ratio = widthR / heightR;
            if (widthR > heightR)
            {
                units[1] = (width / ratio) - Math.Max(0, (width / ratio) - height);
                units[0] = units[1] * ratio;
            }
            else
            {
                units[0] = (height * ratio) - Math.Max(0, (height * ratio) - width);
                units[1] = units[0] / ratio;
            }
            return units;
        }
    }
}

部门类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace WpfApp3
{
    class Sector
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public double Width { get; set; }
        public double Height { get; set; }
        public double Depth { get; set; }
        public int Color { get; set; }
        public long ShelfId { get; set; }

        public SolidColorBrush colorO { get; set; }
    }
}

没有viewbox一切似乎都很好,但ListItems不会调整大小 ds

例如,如果我调整窗口大小,因为在ListView项目下方可以看到高度保持不变,相反它们的高度应该改变并保持与其父级的比率: enter image description here

使用viewbox一切都会失真!!

vb.Child = shelfGrid;
mainG.Children.Add(vb);

enter image description here

如果我使用Stretch.Uniform而不是Stretch.Fill我得到这个:

enter image description here

我缺少什么,或者我应该采取另一种方式来实现这一目标?

0 个答案:

没有答案