Skip to content

Lots of errors occurs when sliding the datagrid scrollbar. #3821

@Sade686

Description

@Sade686

Bug explanation

Lots of errors occurs when sliding the datagrid scrollbar.
*like:
14:45:19:102 System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGridRow', AncestorLevel='1''. BindingExpression:Path=Foreground; DataItem=null; target element is 'DataGridCell' (Name=''); target property is 'Foreground' (type 'Brush')
14:45:19:102 System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=HorizontalGridLinesBrush; DataItem=null; target element is 'DataGridRowHeader' (Name=''); target property is 'BorderBrush' (type 'Brush')
14:45:19:102 System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=GridLinesVisibility; DataItem=null; target element is 'DataGridRowHeader' (Name=''); target property is 'BorderThickness' (type 'Thickness')
*follow is my xaml

<UserControl.Resources>

<ResourceDictionary.MergedDictionaries>
<materialDesign:BundledTheme
BaseTheme="Light"
PrimaryColor="DeepPurple"
SecondaryColor="Lime" />

</ResourceDictionary.MergedDictionaries>

</UserControl.Resources>

<Grid.RowDefinitions>


</Grid.RowDefinitions>
<!--

        <RadioButton
            Margin="4"
            BorderBrush="{DynamicResource PrimaryBrush}"
            IsChecked="{Binding InfoFilter}"
            Style="{StaticResource MaterialDesignTabRadioButton}">
            <StackPanel Orientation="Horizontal">
                <Image
                    Width="16"
                    Height="16"
                    Source="/Assets/Images/Info.png" />
                <TextBlock
                    Margin="5,0"
                    VerticalAlignment="Center"
                    Foreground="Black">
                    <Run Text="Info" />
                    <Run Text="{Binding InfoCount}" />
                </TextBlock>
            </StackPanel>
        </RadioButton>

        <RadioButton
            Margin="4"
            BorderBrush="{DynamicResource PrimaryBrush}"
            IsChecked="{Binding WarnFilter}"
            Style="{StaticResource MaterialDesignTabRadioButton}">
            <StackPanel Orientation="Horizontal">
                <Image
                    Width="16"
                    Height="16"
                    Source="/Assets/Images/Warn.png" />
                <TextBlock
                    Margin="5,0"
                    VerticalAlignment="Center"
                    Foreground="Black">
                    <Run Text="Warn" />
                    <Run Text="{Binding WarnCount}" />
                </TextBlock>
            </StackPanel>
        </RadioButton>
        <RadioButton
            Margin="4"
            BorderBrush="{DynamicResource PrimaryBrush}"
            IsChecked="{Binding ErrorFilter}"
            Style="{StaticResource MaterialDesignTabRadioButton}">
            <StackPanel Orientation="Horizontal">
                <Image
                    Width="16"
                    Height="16"
                    Source="/Assets/Images/Error.png" />
                <TextBlock
                    Margin="5,0"
                    VerticalAlignment="Center"
                    Foreground="Black">
                    <Run Text="Error" />
                    <Run Text="{Binding ErrorCount}" />
                </TextBlock>
            </StackPanel>
        </RadioButton>
        <RadioButton
            Margin="4"
            BorderBrush="{DynamicResource PrimaryBrush}"
            IsChecked="{Binding AlarmFilter}"
            Style="{StaticResource MaterialDesignTabRadioButton}">
            <StackPanel Orientation="Horizontal">
                <Image
                    Width="16"
                    Height="16"
                    Source="/Assets/Images/Alarm.png" />
                <TextBlock
                    Margin="5,0"
                    VerticalAlignment="Center"
                    Foreground="Black">
                    <Run Text="Alarm" />
                    <Run Text="{Binding AlarmCount}" />
                </TextBlock>
            </StackPanel>
        </RadioButton>
    </StackPanel>-->
    <!--<StackPanel
        Margin="2"
        HorizontalAlignment="Right"
        Orientation="Horizontal">
        <Button
            Width="26"
            Height="26"
            Margin="0,0,0,0"
            HorizontalAlignment="Center"
            Click="btnScrollIntoTop_Click"
            Style="{StaticResource MaterialDesignFloatingActionMiniLightButton}">
            <materialDesign:PackIcon Kind="TransferUp" />
        </Button>
        <Button
            Width="26"
            Height="26"
            Margin="2,0,0,0"
            Click="btnScrollIntoEnd_Click"
            Style="{StaticResource MaterialDesignFloatingActionMiniLightButton}">
            <Button.Content>
                <materialDesign:PackIcon Kind="TransferDown" />
            </Button.Content>
        </Button>
    </StackPanel>-->
    <DataGrid
        x:Name="dg"
        Grid.Row="1"
        AutoGenerateColumns="False"
        CanUserAddRows="False"
        EnableColumnVirtualization="True"
        EnableRowVirtualization="True"
        FontSize="10"
        GridLinesVisibility="All"
        HeadersVisibility="Column"
        IsReadOnly="True"
        ItemsSource="{Binding DisplayLogCollection}"
        RowHeaderWidth="20"
        SelectionUnit="FullRow"
        VirtualizingPanel.IsVirtualizing="True">
        <!--<DataGrid.Resources>
            <Style TargetType="DataGridRowHeader">
                <Setter Property="BorderThickness" Value="1"/>
            </Style>
        </DataGrid.Resources>-->
        <!--<DataGrid.RowStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="Foreground" Value="AliceBlue"/>
            </Style>
        </DataGrid.RowStyle>-->
        <DataGrid.ContextMenu>
            <ContextMenu StaysOpen="True">
                <MenuItem Click="ClearAlarm" Header="ClearAlarm" />
            </ContextMenu>
        </DataGrid.ContextMenu>
        <DataGrid.Columns>
            <DataGridTemplateColumn
                Width="150"
                Header="Time"
                IsReadOnly="True">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock HorizontalAlignment="Left" Text="{Binding CreateTime, StringFormat=\{0:yyyy/MM/dd HH:mm:ss fff\}}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn
                Width="80"
                Header="_Type"
                IsReadOnly="True">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock HorizontalAlignment="Left" Text="{Binding LogType}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn
                Width="*"
                Header="_Content"
                IsReadOnly="True">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock
                            HorizontalAlignment="Left"
                            Foreground="{Binding LogColor}"
                            Text="{Binding Content}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

</Grid>
and my .cs public partial class LogView : UserControl { #region Singleton
private static readonly LogView _instance = new LogView();

private LogView()
{
    InitializeComponent();
    this.DataContext = LogViewModel.Ins;
    DispatcherTimer dispatcherTimer = new DispatcherTimer();
    dispatcherTimer.Interval = TimeSpan.FromMilliseconds(100);
    dispatcherTimer.Tick += UpdateUI;
    dispatcherTimer.Start();
}

public static LogView Ins
{
    get { return _instance; }
}

#endregion Singleton

#region Prop

private const int MaxCountDisplayLogCollection = 1999;
private const int MaxCountAllLogCollection = 1999;
private const int MaxCountInfoCollection = 1999;
private const int MaxCountWarnCollection = 1999;
private const int MaxCountErrorCollection = 1999;
private const int MaxCountAlarmCollection = 1999;

#endregion Prop

#region Method

private void UpdateUI(object sender, EventArgs e)
{
    //return;
    while (Logger.LogInfos.Count > 0)
    {
        LogModel logModel = new LogModel();
        if (Logger.LogInfos.TryDequeue(out logModel))
        {
            if (LogViewModel.Ins.DisplayLogCollection.Count >= MaxCountDisplayLogCollection)
            {
                LogViewModel.Ins.DisplayLogCollection.RemoveAt(0);
            }
            if (LogViewModel.Ins.AllLogCollection.Count >= MaxCountAllLogCollection)
            {
                LogViewModel.Ins.AllLogCollection.RemoveAt(0);
            }
            if (LogViewModel.Ins.InfoCollection.Count >= MaxCountInfoCollection)
            {
                LogViewModel.Ins.InfoCollection.RemoveAt(0);
            }
            if (LogViewModel.Ins.WarnCollection.Count >= MaxCountWarnCollection)
            {
                LogViewModel.Ins.WarnCollection.RemoveAt(0);
            }
            if (LogViewModel.Ins.ErrorCollection.Count >= MaxCountErrorCollection)
            {
                LogViewModel.Ins.ErrorCollection.RemoveAt(0);
            }
            if (LogViewModel.Ins.AlarmCollection.Count >= MaxCountAlarmCollection)
            {
                LogViewModel.Ins.AlarmCollection.RemoveAt(0);
            }
            switch (logModel.LogType)
            {
                case MsgType.Success:
                case MsgType.Info:
                    logModel.LogColor = Brushes.Black;
                   
                    break;

                case MsgType.Warn:
                    logModel.LogColor = Brushes.Black;
                   
                    break;

                case MsgType.Error:
                    logModel.LogColor = Brushes.Red;
                    
                    break;

                case MsgType.Alarm:
                    logModel.LogColor = Brushes.Red;
                    break;
            }
            
            LogViewModel.Ins.DisplayLogCollection.Add(logModel);
            
            LogViewModel.Ins.AllLogCollection.Add(logModel);
            LogViewModel.Ins.InfoCount = LogViewModel.Ins.InfoCollection.Count;
            LogViewModel.Ins.WarnCount = LogViewModel.Ins.WarnCollection.Count;
            LogViewModel.Ins.ErrorCount = LogViewModel.Ins.ErrorCollection.Count;
            LogViewModel.Ins.AlarmCount = LogViewModel.Ins.AlarmCollection.Count;
        }
        if (Logger.LogInfos.Count == 0)
        {
            ScrollIntoEnd();
        }
    }
}

private void btnScrollIntoTop_Click(object sender, RoutedEventArgs e)
{
    //if (dg.Items.Count > 0)
    //{
    //    dg.ScrollIntoView(dg.Items[0]);
    //}
}

private void btnScrollIntoEnd_Click(object sender, RoutedEventArgs e)
{
    //if (dg.Items.Count > 0)
    //{
    //    dg.Items.Refresh();
    //    //dataGrid.ScrollIntoView(targetItem);

    //    dg.ScrollIntoView(dg.Items[dg.Items.Count - 1]);
    //}
}

private void ScrollIntoEnd()
{
    return;
    //if (dg.Items.Count > 0)
    //{
    //    Dispatcher.BeginInvoke(() =>
    //    {
    //        dg.ScrollIntoView(dg.Items[dg.Items.Count - 1]);
    //    });
        
    //}
}

private void ClearAlarm(object sender, RoutedEventArgs e)
{
    LogViewModel.Ins.AlarmCollection.Clear();
    LogViewModel.Ins.AlarmCount = LogViewModel.Ins.AlarmCollection.Count;
   
}

#endregion Method

}

Version

5.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugevaluation requiredItems is pending review or evaluation by the team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions