C# 当我上下滚动时,WPF项目中DataGrid的单元格文本被清除
我有一个包含DataGrid的WPF项目,当程序启动时,我用许多行填充它,然后在每个按钮上单击我设置它的一个单元格的内容。 问题是,如果我在DG中上下滚动,单元格的内容似乎消失了! 请帮助我确定问题以及解决方法 XAML:C# 当我上下滚动时,WPF项目中DataGrid的单元格文本被清除,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,我有一个包含DataGrid的WPF项目,当程序启动时,我用许多行填充它,然后在每个按钮上单击我设置它的一个单元格的内容。 问题是,如果我在DG中上下滚动,单元格的内容似乎消失了! 请帮助我确定问题以及解决方法 XAML: <Window x:Class="WpfTest1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http:/
<Window x:Class="WpfTest1.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">
<Grid>
<DockPanel Grid.Column="1" Margin="0,10,1,0" Panel.ZIndex="10" MinWidth="437" MinHeight="38" VerticalAlignment="Top" HorizontalAlignment="Right">
<DataGrid x:Name="DGV" HorizontalAlignment="Stretch" VerticalAlignment="Top" Height="103" Panel.ZIndex="1" RowBackground="#FFC6C6C6" FontWeight="Bold" CanUserSortColumns="False" CanUserResizeColumns="False" CanUserReorderColumns="False" MinColumnWidth="0" IsReadOnly="False" AlternatingRowBackground="Gainsboro" AlternationCount="2">
<DataGrid.Columns>
<DataGridTextColumn Header=" "/>
<DataGridHyperlinkColumn Header="URL" Width="295" Binding="{Binding url}"/>
<DataGridTextColumn Header="Ahrefs(http)" Width="79" Binding="{Binding AhrefsHttp}"/>
<DataGridTextColumn Header="Ahrefs(www)" Width="83" Binding="{Binding AhrefsWww}"/>
<DataGridTextColumn Header="Archive" Width="79" Binding="{Binding Archive}"/>
<DataGridTextColumn Header="Majesticseo" Width="79" Binding="{Binding Majesticseo}"/>
<DataGridTextColumn Header="Majesticseo(anch historic)" Width="79" Binding="{Binding Majesticseo}"/>
<DataGridTextColumn Header="Majesticseo(ref historic)" Width="79" Binding="{Binding Majesticseo}"/>
</DataGrid.Columns>
</DataGrid>
</DockPanel>
<Button Content="Button" HorizontalAlignment="Left" Margin="0,118,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
</Window>
这是代码:
using System;
using System.Collections.Generic;
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 WpfTest1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
int selectedIndex;
public class myLink
{
private string Url;
public string url
{
get { return Url; }
set { Url = value; }
}
private string ahrefsHttp;
public string AhrefsHttp
{
get { return ahrefsHttp; }
set { ahrefsHttp = value; }
}
private string ahrefsWww;
public string AhrefsWww
{
get { return ahrefsWww; }
set { ahrefsWww = value; }
}
private string archive;
public string Archive
{
get { return archive; }
set { archive = value; }
}
private string majesticseo;
public string Majesticseo
{
get { return majesticseo; }
set { majesticseo = value; }
}
private string majesticseoAnchorHistoric;
public string MajesticseoAnchorHistoric
{
get { return majesticseoAnchorHistoric; }
set { majesticseoAnchorHistoric = value; }
}
private string majesticseoReferringHistoric;
public string MajesticseoReferringHistoric
{
get { return majesticseoReferringHistoric; }
set { majesticseoReferringHistoric = value; }
}
}
public MainWindow()
{
InitializeComponent();
for (int i = 1; i <= 10; i++)
AddNewRow("test");
}
private void AddNewRow(string txt)
{
myLink dm = new myLink { url = txt, AhrefsHttp = "", AhrefsWww = "", Archive = "", Majesticseo = "" };
DGV.Items.Add(dm);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SetLinkStateInDG("Good");
}
private void SetLinkStateInDG(string state)
{
DGV.SelectedItem = DGV.Items[selectedIndex++];
if (state != "")
{
string cmt = Microsoft.VisualBasic.Interaction.InputBox("Any comment?", "Comment", "", -1, -1);
if (cmt != "")
state = state + " (" + cmt + ")";
}
foreach (DataGridCellInfo cellInfo in DGV.SelectedCells)
{
// this changes the cell's content not the data item behind it
if (cellInfo.Column.Header.ToString() == "Majesticseo(anch historic)")
{
DataGridCell gridCell = TryToFindGridCell(DGV, cellInfo);
if (gridCell != null) gridCell.Content = state;
break;
}
}
DGV.SelectedItem = DGV.Items[selectedIndex];
}
static DataGridCell TryToFindGridCell(DataGrid grid, DataGridCellInfo cellInfo)
{
DataGridCell result = null;
DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(cellInfo.Item);
if (row != null)
{
int columnIndex = grid.Columns.IndexOf(cellInfo.Column);
if (columnIndex > -1)
{
System.Windows.Controls.Primitives.DataGridCellsPresenter presenter = GetVisualChild<System.Windows.Controls.Primitives.DataGridCellsPresenter>(row);
result = presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex) as DataGridCell;
}
}
return result;
}
static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
}
}
使用系统;
使用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;
命名空间WpfTest1
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
int-selectedIndex;
公共类myLink
{
私有字符串Url;
公共字符串url
{
获取{返回Url;}
设置{Url=value;}
}
私有字符串ahrefsHttp;
公共字符串AhrefsHttp
{
获取{return ahrefsHttp;}
设置{ahrefsHttp=value;}
}
私有字符串ahrefsWww;
公共字符串AHREFSWW
{
获取{return ahrefsWww;}
设置{ahrefsWww=value;}
}
私人字符串档案;
公共字符串存档
{
获取{return archive;}
设置{archive=value;}
}
私人串雄伟;
公共字符串Majesticseo
{
获取{return majesticseo;}
设置{majesticseo=value;}
}
私人字符串雄伟的地理主播历史;
公共字符串MajesticseoanchorHistorical
{
获取{return majesticseoanchorhistic;}
设置{majesticseoanchorhistic=value;}
}
私人字符串雄伟的地理参考历史;
公共字符串majesticseorefereringhistorical
{
获取{return majesticseorefereringhistorical;}
设置{majesticseorefereringhistorical=value;}
}
}
公共主窗口()
{
初始化组件();
对于(int i=1;i对不起,我找到了解决方案,似乎我必须为DataGrid设置一个属性来修复它:
ScrollViewer.CanContentScroll="False"