c# - 如何在选择时修复 WPF 数据网格上移动的单元格?
问题描述
如您所见,所选单元格向侧面移动。我该如何解决?
这是最低限度的代码:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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 test
{
public partial class MainWindow : Window
{
public event PropertyChangedEventHandler PropertyChanged;
protected void SetField<T> ( ref T field, T value, string propertyName )
{
if ( !EqualityComparer<T>.Default.Equals ( field, value ) )
{
field = value;
PropertyChanged?.Invoke ( this, new PropertyChangedEventArgs ( propertyName ) );
}
}
ObservableCollection<Coin> _coins;
public ObservableCollection<Coin> Coins { get => _coins; set => SetField ( ref _coins, value, nameof ( _coins ) ); }
public ICollectionView CollectionView;
public MainWindow ( )
{
this.Coins = new ObservableCollection<Coin> ( );
this.Coins.Add ( new Coin ( "Coin 1" ) );
this.Coins.Add ( new Coin ( "Coin 2" ) );
this.Coins.Add ( new Coin ( "Coin 3" ) );
this.Coins.Add ( new Coin ( "Coin 4" ) );
this.Coins.Add ( new Coin ( "Coin 5" ) );
this.DataContext = this;
InitializeComponent ( );
}
}
public class Coin
{
public string Symbol { get; set; }
public SolidColorBrush Color2 { get; set; }
public Coin ( string symbol )
{
this.Symbol = symbol;
this.Color2 = new SolidColorBrush ( Color.FromRgb ( 0, 200, 0 ) );
}
}
}
xml:
<Window x:Class="test.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:test"
mc:Ignorable="d"
SizeToContent="Width"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="test"
WindowStyle="None"
AllowsTransparency="True"
Topmost="True"
Height="426">
<Window.Resources>
<Style x:Key="DataGridColumnSeparatorStyle" TargetType="DataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Rectangle VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Fill="Red"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="DataGridColumnAlarmStyle" TargetType="DataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Rectangle VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Fill="#000000"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Stretch"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Column.Header, RelativeSource={RelativeSource Self}}" Value="Symbol"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="DataGridCell.Background" Value="{Binding Path=Color2}" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
<Style x:Key="RowStyleWithAlternation" TargetType="DataGridRow">
<Setter Property="Background" Value="DarkKhaki"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontWeight" Value="Normal"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="BorderBrush" Value="DarkMagenta" />
<Setter Property="BorderThickness" Value="2" />
<Setter Property="Background" Value="Yellow"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<DataGrid Name="dataGrid" ItemsSource="{Binding Coins}" SelectionMode="Single" GridLinesVisibility="None" HorizontalScrollBarVisibility="Hidden" RowHeaderWidth="0" IsReadOnly="True" CanUserAddRows="False" CanUserResizeColumns="False" CanUserResizeRows="False" AutoGenerateColumns="False" RowStyle="{StaticResource RowStyleWithAlternation}">
<DataGrid.Resources>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header=" " MinWidth="0" Width="10" CanUserSort="False"/>
<DataGridTemplateColumn MinWidth="0" Width="2" CellStyle="{StaticResource DataGridColumnSeparatorStyle}"/>
<DataGridTextColumn Header="Symbol" Width="64" Binding="{Binding Path=Symbol}" />
<DataGridTemplateColumn MinWidth="0" Width="2" CellStyle="{StaticResource DataGridColumnSeparatorStyle}" CanUserSort="False"/>
<DataGridTextColumn Header="PNL %" Width="64" Binding="{Binding Path=Color}"/>
<DataGridTextColumn Width="64" Binding="{Binding Path=PriceChangeInPercentDailyDisplay}">
<DataGridTextColumn.Header>
<TextBlock Text="Profit"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
解决方案
推荐阅读
- php - 我在我的 php 上传脚本中做错了什么?
- python-3.x - 使用“for”循环在 Python 中打印模式
- azure - 我用西班牙语创建了一个知识库,但机器人用英语响应
- performance - 原始类型 HashSet 或 HashMap 比 Object 快 10 倍?
- javascript - SyntaxError:jquery.datatables 中出现意外的标记“{”
- node.js - 运行 Node.js 时找不到模块
- azure-functions - Azure 函数中非常简单的状态变量
- openlayers - 移动 OpenLayers 地图时特征模糊
- mysql - 我们可以在 dialogflow 上制作价格可协商的聊天机器人吗?
- c++ - 使用初始化列表作为函数参数实现 operator[] 的类对象示例