編輯:我已經被帶領注意到問題不在第一行,但所有行都在鼠標滾動後移動。Datagrid滾動時奇怪地行事
我非常清楚,這是一個棘手的問題,這可能會導致我的不完整或者太私人的問題。這就是說我會盡可能清楚地解釋它。 我有一個由我通過例程構建的數據網格。如有需要,我可以分享。在此之後,通過數據網格發生的事件轉變:
<DataGrid Name="dtgResults" Background="Transparent" CanUserSortColumns="False" IsReadOnly="True" Foreground="Black" LoadingRow="Datagrid_LoadingRow">
<DataGrid.Resources>
<Style TargetType="DataGridCell">
<EventSetter Event="DataGridCell.Loaded" Handler="DataGridCell_Load"/>
<Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
<Setter Property="HorizontalAlignment" Value="Right" />
</Style>
</DataGrid.Resources>
</DataGrid>
那就是:
在Datagrid_LoadingRow
int index = row.GetIndex();
if (obcMyDim[index].IsComment) <-----a matrix telling when it's a comment
row.Foreground = new SolidColorBrush(Colors.White);
else
row.Foreground = new SolidColorBrush(Colors.Black);
所以它只是一個色素無電池數據的變化!
在DataGridCell_Load
,而不是I格式的小數位數,並通過這樣的:
DataGridCell cell = sender as DataGridCell;
DataGrid parentDataGrid = (DataGrid)(cell.Tag);
int column = cell.Column.DisplayIndex;// da 0
int row = -1;
//I get the line
DependencyObject rowDO = cell as DependencyObject;
while (rowDO != null && !(rowDO is DataGridRow))
rowDO = VisualTreeHelper.GetParent(rowDO);
if (rowDO != null)
{
DataGridRow gridrow = (DataGridRow)rowDO;
row = gridrow.GetIndex();
}
...
string strCellContent = ((TextBlock)cell.Content).Text;
if (strCellContent.Trim() != string.Empty)
{
if (strCellContent.IsNumeric())<----custom function
{
dVal = double.Parse(strCellContent, CultureInfo.InvariantCulture);
((TextBlock)cell.Content).Text = dVal.ToString("0.0000");
bool condOutOfTolerance = ...;
//Errore
if (column >= 5 && condOutOfTolerance)
cell.Foreground = new SolidColorBrush(Colors.Red);
}
}
因此,在短期的問題是,第一行的行爲古怪
後,然後有滾動向上/向下 DataGrid中的第一行改變
,然後再以不同
我得到那個不知何故它的感覺一個刷新的問題,但我不明白如何解決這個問題。
感謝您的幫助
帕特里克 ---- ----新增
下面是我手工填充我的DataGrid:
public void SynchronizeDtgResults(DataGrid dtg)
{
/*-----------------------------------------------*
* manuale generation HEADER/COLUMNS/ROWS *
*-----------------------------------------------*/
if (obcMyDim == null || obcMyDim.Count() == 0 || obcMyDim[0].obcItemsName == null || obcMyDim[0].obcItemsName.Count() == 0)
{
Serializers.Logger.WriteLog("WARNING SynchronizeDtgResults called with obcmMyDim not valid");
return;
}
int numColonneFisse = 5; //Num, name, nominal, ut, ul
int numParts = obcMyDim[0].obcItemsName.Count();//refer to the first coz are all the same
int numTotaleColonne = numColonneFisse + numParts;
int numTotaleRighe = obcMyDim.Count;
string[] columnLabels = new string[numTotaleColonne];
//1. Header Preparation
// first 5 fixed col
columnLabels[0] = Languages.Word(eWords.Num);
columnLabels[1] = Languages.Word(eWords.Name);
columnLabels[2] = Languages.Word(eWords.Nominal);
columnLabels[3] = Languages.Word(eWords.UT);
columnLabels[4] = Languages.Word(eWords.LT);
// and then the names of the parts
for (int iii = 0; iii < numParts; iii++)
columnLabels[iii + numColonneFisse] = obcMyDim[0].obcItemsName[iii];
//2. Clean and add header
Application.Current.Dispatcher.Invoke((Action)(() => { dtg.Columns.Clear(); }));
Application.Current.Dispatcher.Invoke((Action)(() => { dtg.Items.Clear(); }));
var styleHeaderDatagrid = new Style(typeof(System.Windows.Controls.Primitives.DataGridColumnHeader));
styleHeaderDatagrid.Setters.Add(new Setter { Property = BorderBrushProperty, Value = Brushes.Black });
styleHeaderDatagrid.Setters.Add(new Setter { Property = BorderThicknessProperty, Value = new Thickness(1) });
styleHeaderDatagrid.Setters.Add(new Setter { Property = ForegroundProperty, Value = Brushes.Black });
styleHeaderDatagrid.Setters.Add(new Setter { Property = FontWeightProperty, Value = FontWeights.Bold });
styleHeaderDatagrid.Setters.Add(new Setter { Property = MarginProperty, Value = new Thickness(2) });
var tb = new TextBlock() { FontSize = dtg.FontSize, FontFamily = dtg.FontFamily };
reportDimensionNameWidth = 0;
foreach (var item in obcMyDim)
{
tb.Text = item.NameAxisDimension;
double dim = GraphicExtensions.GetTextBlockSize(tb).Width;
if (dim > reportDimensionNameWidth)
reportDimensionNameWidth = dim + 20;
}
foreach (string label in columnLabels)
{
DataGridTextColumn column = new DataGridTextColumn();
column.Header = label;
column.HeaderStyle = styleHeaderDatagrid;
column.Binding = new Binding(label.Replace(' ', '_'));
column.Width = new DataGridLength(1, DataGridLengthUnitType.Auto);
Application.Current.Dispatcher.Invoke((Action)(() => { dtg.Columns.Add(column); }));
}
//3 adding rows
int num = 1;
for (int riga = 0; riga < numTotaleRighe; riga++)
{
dynamic newRow = new ExpandoObject();
//Adding dimension per each row
for (int colonna = 0; colonna < numTotaleColonne; colonna++)
{
string strColumnHeader = columnLabels[colonna].Replace(' ', '_');
((IDictionary<String, Object>)newRow)[strColumnHeader] = string.Empty;
switch (colonna)
{
//Here fixed lines
case 0: if (!obcMyDim[riga].IsComment) ((IDictionary<String, Object>)newRow)[strColumnHeader] = num++; break;
case 1: ((IDictionary<String, Object>)newRow)[strColumnHeader] = obcMyDim[riga].NameAxisDimension; break;
case 2: if (!obcMyDim[riga].IsComment) ((IDictionary<String, Object>)newRow)[strColumnHeader] = obcMyDim[riga].Nominal; break;
case 3: if (!obcMyDim[riga].IsComment) ((IDictionary<String, Object>)newRow)[strColumnHeader] = obcMyDim[riga].UpperTolerance; break;
case 4: if (!obcMyDim[riga].IsComment) ((IDictionary<String, Object>)newRow)[strColumnHeader] = obcMyDim[riga].LowerTolerance; break;
//Here all data
default:
if (!obcMyDim[riga].IsComment)
{
if (colonna < numTotaleColonne)
{
if ((colonna - numColonneFisse < 0) || (colonna - numColonneFisse) > obcMyDim[riga].obcItemsMeasured.Count)
{
string str = "Wrong num column obcMyDim idx=" + (colonna - numColonneFisse) + " obcmMyDim[" + riga + "].obcItemsMeasured.Count= " + obcMyDim[riga].obcItemsMeasured.Count;
MessageBox.Show(str);
}
else
((IDictionary<String, Object>)newRow)[strColumnHeader] = (obcMyDim[riga].obcItemsMeasured[colonna - numColonneFisse]).ToString();
}
else
MessageBox.Show("DtgResult Wrong column number: " + colonna);
}
break;
}
}
Serializers.Logger.WriteLog("SynchronizeDtgResults added new row num=" + riga + " name=" + obcMyDim[riga].NameAxisDimension);
Application.Current.Dispatcher.Invoke((Action)(() => { dtg.Items.Add(newRow); }));
}
Serializers.Logger.WriteLog("SynchronizeDtgResults END");
}
再次感謝您幫助! !
其他行上的值也在移動(其中一些改變了?) – stuartd
OMG我沒有注意到它!!!!!是的,我可以注意到所有價值線的上移...... – Patrick