当我向下和向上滚动时,WPF项目中DataGrid的单元格文本正在被清除



我有一个包含DataGrid的WPF项目,当程序启动时,我用许多行填充它,然后在每个按钮上单击我设置它的单元格内容。问题是,如果我在DG中上下滚动,单元格的内容似乎就不见了!请帮助我识别问题以及如何解决它。

XAML:

<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;
        }
    }
}

很抱歉,我找到了解决方案,似乎我必须为DataGrid设置一个属性来修复它:

ScrollViewer.CanContentScroll="False"

相关内容

  • 没有找到相关文章

最新更新