Wpf Datagrid-滚动将水平裁剪图像,而不是垂直裁剪图像
我需要反转DataGrid上的列/行(请参见和) 一旦我将其反转,我就会对数据网格中显示的图像产生一些奇怪的效果 当我向下滚动时,第1列将在左侧和右侧分别裁剪图像。我越往下走,它就越往左边走,直到什么都没有了 我怎样才能解决这个问题 这里是一个完整的简单示例,如果您想测试它(您只需要将其复制/粘贴到新项目中,然后向下滚动查看问题) MainWindows.xamlWpf Datagrid-滚动将水平裁剪图像,而不是垂直裁剪图像,wpf,image,datagrid,scrollviewer,Wpf,Image,Datagrid,Scrollviewer,我需要反转DataGrid上的列/行(请参见和) 一旦我将其反转,我就会对数据网格中显示的图像产生一些奇怪的效果 当我向下滚动时,第1列将在左侧和右侧分别裁剪图像。我越往下走,它就越往左边走,直到什么都没有了 我怎样才能解决这个问题 这里是一个完整的简单示例,如果您想测试它(您只需要将其复制/粘贴到新项目中,然后向下滚动查看问题) MainWindows.xaml <Window x:Class="RotatedDataGrid.MainWindow" xmln
<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
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间旋转数据网格
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共ICollectionView客户{get;private set;}
public ICollectionView GroupedCustomers{get;private set;}
公共主窗口()
{
初始化组件();
var_客户=新列表
{
新客户
{
FirstName=“Christian”,
LastName=“Moser”,
性别=性别。男性,
网站=新的Uri(“http://www.wpftutorial.net"),
ReceiveNewsletter=true,
Image=“Images/christian.jpg”
},
新客户
{
FirstName=“彼得”,
LastName=“Meyer”,
性别=性别。男性,
网站=新的Uri(“http://www.petermeyer.com"),
Image=“Images/peter.jpg”
},
新客户
{
FirstName=“Lisa”,
LastName=“辛普森”,
性别=性别。女性,
网站=新的Uri(“http://www.thesimpsons.com"),
Image=“Images/lisa.jpg”
},
新客户
{
FirstName=“贝蒂”,
LastName=“Bossy”,
性别=性别。女性,
网站=新的Uri(“http://www.bettybossy.ch"),
Image=“Images/betty.jpg”
}
};
Customers=CollectionViewSource.GetDefaultView(_Customers);
GroupedCustomers=新的ListCollectionView(\u客户);
GroupedCustomers.GroupDescriptions.Add(新属性GroupDescription(“性别”);
MyRotatedDataGrid.DataContext=this;
}
}
公共枚举性别
{
男性的
女的
}
公共类客户:INotifyPropertyChanged
{
私有字符串_firstName;
私有字符串_lastName;
私人性别;
私人Uri_网站;
私人布尔大学通讯;
私有字符串图像;
公共字符串名
{
获取{return\u firstName;}
设置
{
_firstName=值;
NotifyPropertyChanged(“名字”);
}
}
最后一个公共字符串
<Style TargetType="DataGridCell" BasedOn="{StaticResource DataGridBase}"/>
<Style TargetType="DataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid>
<Grid.LayoutTransform>
<TransformGroup>
<RotateTransform Angle="90" />
<MatrixTransform Matrix="-1, 0, 0, 1, 0, 0" />
</TransformGroup>
</Grid.LayoutTransform>
<ContentPresenter/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>