Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Sade686 opened this issue Mar 20, 2025 · 1 comment
Open

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

Sade686 opened this issue Mar 20, 2025 · 1 comment
Labels
bug evaluation required Items is pending review or evaluation by the team

Comments

@Sade686
Copy link

Sade686 commented Mar 20, 2025

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

@Sade686 Sade686 added bug evaluation required Items is pending review or evaluation by the team labels Mar 20, 2025
@corvinsz
Copy link
Member

@Sade686 is it possible you provide either a repo, or a different, more to the point example?
With the code you provided above I have to change alot of things to even get it compiled, and I feel like it contains alot of other code which is not part of the problem you are trying to report.
This makes it hard to reproduce and thus pinpoint the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug evaluation required Items is pending review or evaluation by the team
Projects
None yet
Development

No branches or pull requests

2 participants