数据网格 - 滚动将水平裁剪图像而不是垂直裁剪

时间:2015-09-30 23:45:35

标签: wpf image datagrid scrollviewer

我需要在DataGrid上反转列/行(请参阅WPF horizontal DataGridRotatedDataGrid

一旦我将它倒置,我对数据网格内显示的图像产生了一些奇怪的效果。

当我向下滚动时,第1列将在左侧裁剪图像,在右侧裁剪一点。我越往下走,它就越多,它就会越多,直到没有更多的东西。

enter image description here

enter image description here

我该如何解决?

这是一个完整的简单示例,如果你想测试它(你只需要在新项目中复制/粘贴它并向下滚动以查看问题)

MainWindows.xaml

    <Window x:Class="RotatedDataGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="150" Width="1000">
    <Grid>
        <DataGrid x:Name="MyRotatedDataGrid" HorizontalContentAlignment="Center"
                     ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" 
                     AutoGenerateColumns="True"
                     ItemsSource="{Binding Customers}">
            <DataGrid.Resources>
                <Style x:Key="DataGridBase" TargetType="Control">
                    <Setter Property="LayoutTransform">
                        <Setter.Value>
                            <TransformGroup>
                                <RotateTransform Angle="-90" />
                                <ScaleTransform ScaleX="1" ScaleY="-1" />
                            </TransformGroup>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="TextOptions.TextFormattingMode" Value="Display" />
                </Style >
                <Style TargetType="DataGridCell" BasedOn="{StaticResource DataGridBase}"/>
                <Style TargetType="DataGridColumnHeader" BasedOn="{StaticResource DataGridBase}"/>
                <Style TargetType="DataGridRowHeader" BasedOn="{StaticResource DataGridBase}"/>
            </DataGrid.Resources>

            <DataGrid.LayoutTransform>
                <TransformGroup>
                    <RotateTransform Angle="90" />
                    <MatrixTransform Matrix="-1, 0, 0, 1, 0, 0" />
                </TransformGroup>
            </DataGrid.LayoutTransform>

            <DataGrid.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Padding="3"/>
                            </StackPanel>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Expander>
                                            <Expander.Header>
                                                <StackPanel Orientation="Horizontal">
                                                    <TextBlock Text="{Binding Path=Name}"/>
                                                    <TextBlock Text="{Binding Path=ItemCount}" Margin="8,0,4,0"/>
                                                    <TextBlock Text="Items"/>
                                                </StackPanel>
                                            </Expander.Header>
                                            <ItemsPresenter />
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </DataGrid.GroupStyle>

        </DataGrid>
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
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 RotatedDataGrid
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ICollectionView Customers { get; private set; }
        public ICollectionView GroupedCustomers { get; private set; }


        public MainWindow()
        {
            InitializeComponent();




            var _customers = new List<Customer>
                                 {
                                     new Customer
                                         {
                                             FirstName = "Christian",
                                             LastName = "Moser",
                                             Gender = Gender.Male,
                                             WebSite = new Uri("http://www.wpftutorial.net"),
                                             ReceiveNewsletter = true,
                                             Image = "Images/christian.jpg"
                                         },
                                     new Customer
                                         {
                                             FirstName = "Peter",
                                             LastName = "Meyer",
                                             Gender = Gender.Male,
                                             WebSite = new Uri("http://www.petermeyer.com"),
                                             Image = "Images/peter.jpg"
                                         },
                                     new Customer
                                         {
                                             FirstName = "Lisa",
                                             LastName = "Simpson",
                                             Gender = Gender.Female,
                                             WebSite = new Uri("http://www.thesimpsons.com"),
                                             Image = "Images/lisa.jpg"
                                         },
                                     new Customer
                                         {
                                             FirstName = "Betty",
                                             LastName = "Bossy",
                                             Gender = Gender.Female,
                                             WebSite = new Uri("http://www.bettybossy.ch"),
                                             Image = "Images/betty.jpg"
                                         }
                                 };

            Customers = CollectionViewSource.GetDefaultView(_customers);

            GroupedCustomers = new ListCollectionView(_customers);
            GroupedCustomers.GroupDescriptions.Add(new PropertyGroupDescription("Gender"));


            MyRotatedDataGrid.DataContext = this;
        }
    }




     public enum Gender
    {
        Male, 
        Female
    }

     public class Customer : INotifyPropertyChanged
     {
         private string _firstName;
         private string _lastName;
         private Gender _gender;
         private Uri _webSite;
         private bool _newsletter;
         private string _image;

         public string FirstName
         {
             get { return _firstName; }
             set
             {
                 _firstName = value;
                 NotifyPropertyChanged("FirstName");
             }
         }

         public string LastName
         {
             get { return _lastName; }
             set
             {
                 _lastName = value;
                 NotifyPropertyChanged("LastName");
             }
         }

         public Gender Gender
         {
             get { return _gender; }
             set
             {
                 _gender = value;
                 NotifyPropertyChanged("Gender");
             }
         }

         public Uri WebSite
         {
             get { return _webSite; }
             set
             {
                 _webSite = value;
                 NotifyPropertyChanged("WebSite");
             }
         }

         public bool ReceiveNewsletter
         {
             get { return _newsletter; }
             set
             {
                 _newsletter = value;
                 NotifyPropertyChanged("Newsletter");
             }
         }

         public string Image
         {
             get { return _image; }
             set
             {
                 _image = value;
                 NotifyPropertyChanged("Image");
             }
         }

         #region INotifyPropertyChanged Members

         public event PropertyChangedEventHandler PropertyChanged;

         #endregion

         #region Private Helpers

         private void NotifyPropertyChanged(string propertyName)
         {
             if (PropertyChanged != null)
             {
                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
             }
         }

         #endregion
     }
}

1 个答案:

答案 0 :(得分:0)

好的我找到了解决方法。 应用到DataGridCell的转换正在创建此scrollviewer问题。为了解决这个问题,我删除了DataGridCell上的布局转换(通过删除BaseOn代码),并将转换应用到DataGridCell模板中。

WRONG

assume CS:code, DS:data
data segment
    a dw 1
    b db 2
    c dd 3
    x dd ?
data ends
code segment
    start:
        mov ax, data
        mov ds, ax

        mov ax, a   ; AX = a
        mul a       ; AX = a*a
        add ax, 2   ; AX = a*a + 2

        mov bl, b   ; BX = b
        add bl, b   ; BX = b*b  
        div bl      ; a*a+b/b+b
        mov ax, word ptr c+2
        mov dx, word ptr c
        add ax, word ptr c
        mov word ptr x+2, dx
        mov word ptr x, ax

        mov ax, 4c00h
        int 21h
    code ends
end start

RIGHT

<Style TargetType="DataGridCell" BasedOn="{StaticResource DataGridBase}"/>