DeferRefresh is not allowed during an AddNew or EditItem transaction - c#

I have a Window containing 2 DataGrids. And if I click from one specific column in the first DataGrid into any column of the other DataGrid, then I get the error
DeferRefresh is not allowed during an AddNew or EditItem transaction
What is going wrong here?
The first DataGrid is
<DataGrid x:Name="FirstDataGrid"
ItemsSource="{Binding Parts, Mode=TwoWay}"
SelectedItem="{Binding SelectedPart, Mode=TwoWay}"
CellEditEnding="DataGrid_OnCellEditEnding" >
<i:Interaction.Behaviors>
<views:ScrollIntoViewBehavior />
</i:Interaction.Behaviors>
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Identifications, Mode=OneWay}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Margin="5,0,0,0">
<Hyperlink NavigateUri="{Binding ArticleNumber, Mode=OneWay}"
Command="{Binding ElementName=PartDataGrid, Path=DataContext.OpenIdentificationCommand}" CommandParameter="{Binding}" >
<TextBlock Text="{Binding ArticleNumber, Mode=OneWay}"/>
</Hyperlink>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
The second DataGrid does not matter since I can click on any column to produce the error.

Solved it by myself.
The column that causes problems is read-only. So why allow an edit mode anyway? That is obviously wrong. I fixed that by adding
IsReadOnly="True"
to the WPF column definition.

Related

Wpf ComboBox with Checkbox Performance Problems

I've got a WPF Combobox with about 1000 rows.
Each Row has a checkbox to allow selecting mulltiple rows.
If nothing has been selected it opens just fine. Howeve if I've already selected something and now want to change my selection it takes about 30s for the ComboBox to open and it has for some reason scrolled to the bottom.
I'm using VirtualizingStackPanel but it doesn't seem to fix the problem.
<ComboBox
x:Name="cbStockCat"
Grid.Column="1"
Margin="2,0,2,0"
Padding="2,10,0,4"
materialDesign:HintAssist.Hint="Stock Category"
materialDesign:HintAssist.IsFloating="True"
StaysOpenOnEdit="True"
customProperties:EnterKeyTraversalExtension.IsEnabled="True"
AllowDrop="True"
IsEditable="True"
ToolTip="{Binding StockCategoryCollection.ToolTipText}"
TabIndex="{Binding StockCatTabIndex, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
Text="{Binding StockCategoryCollection.Text, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding StockCategoryCollection}"
Cursor="Hand">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<ComboBox.ItemTemplate>
<DataTemplate >
<CheckBox
IsChecked="{Binding IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Content="{Binding}"
Background="{StaticResource BrushPrimaryLighter}"
CommandParameter="{Binding}" >
</CheckBox>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Any ideas?

How to get rid of border in datagrid cell content?

I am using regular WPF datagrid.
I want to get rid of the anoying border around the item in cell. Which property should I use to remove it?
I am actually using celltemplate to set the same behavior in column.
[Annoying border] https://i.ibb.co/ryxTfYB/anoyingborder.png
<DataGrid.Columns>
<DataGridTemplateColumn Header="Field" Width="130" >
<DataGridTemplateColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
</Style>
</DataGridTemplateColumn.CellStyle>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="FieldType" IsEditable="True"
ItemsSource="{Binding FieldTypeComboList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
DisplayMemberPath="Value"
SelectedItem="{Binding Type, Mode=TwoWay}"
VerticalAlignment="Center"
HorizontalContentAlignment="Center"
IsTextSearchEnabled="True"
MaxDropDownHeight="300">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel VirtualizingPanel.IsVirtualizing="True" IsItemsHost="True"/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
try setting BorderThickness="0" on combobox and match the combobox height with the cells height
Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridCell}, Path=ActualHeight}"

WPF Datagrid virtualization getting disabled when placed in a scrollviewer

I have a scenario where in I am using two datagrids basically to achieve grouping as shown below. For achieving synchronized scrolling between two grids I am using a scrollviewer. Using scroll viewer is disabling the virtualization and loading data in datagrids is getting affected drastically. below is the sample code I am using. Any suggestions whould be of gr8 help.
<Grid Name="Test" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<DataGrid Name="DataGrid1" Grid.Column="0" AutoGenerateColumns="False" CanUserResizeRows="False"
CanUserAddRows="False" BorderBrush="#FF1B1A1A" BorderThickness="0" Background="{x:Null}" HorizontalGridLinesBrush="DarkGray"
VerticalGridLinesBrush="DarkGray" SelectionChanged="basketNameDataGrid_SelectionChanged" PreviewMouseWheel="basketNameDataGrid_PreviewMouseWheel">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Background"
Value="#1E90FF" />
<Setter Property="Foreground"
Value="White" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Height" Value="{Binding RowHeight}"></Setter>
</Style>
</DataGrid.RowStyle>
<!--<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Height" Value="{Binding RowHeight}"></Setter>
</Style>
</DataGrid.RowStyle>-->
<DataGrid.Columns>
<!--<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" >
<Border Margin=".5" >
<Button Content="Signal" KeyboardNavigation.IsTabStop="False"/>
</Border>
<StackPanel Orientation="Horizontal">
<Border Margin="0" >
<Button Margin="0" >
<TextBlock Text="Name" />
</Button>
</Border>
<Border >
<Button KeyboardNavigation.IsTabStop="False" >
<TextBlock MinWidth="90" MinHeight="27" Text="Description" Margin="10,10,0,0" />
</Button>
</Border>
</StackPanel>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
</DataGridTemplateColumn>-->
<DataGridTemplateColumn Header="Object Reference" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ObjRef}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="by ICD">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ByICD}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="by Standard" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ByStandard}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<DataGrid Name="DataGrid2" Grid.Column="1" HeadersVisibility="Column"
CanUserResizeRows="False" CanUserAddRows="False" Margin="0" BorderBrush="{x:Null}" BorderThickness="0" Background="{x:Null}"
HorizontalGridLinesBrush="DarkGray" VerticalGridLinesBrush="DarkGray" SelectionChanged="itemDataGrid_SelectionChanged" PreviewMouseWheel="itemDataGrid_PreviewMouseWheel">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Background"
Value="#1E90FF" />
<Setter Property="Foreground"
Value="White" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Height" Value="20"></Setter>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Attribute" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Attribute, Mode = OneWay}" HorizontalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Type" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Type, Mode = OneWay}" HorizontalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="FC" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding FC, Mode = OneWay}" HorizontalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Default" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding DefaultVal, Mode = OneWay}" HorizontalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Table Type" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding TableType, Mode = OneWay}" HorizontalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Signal Name" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name, Mode = OneWay}" HorizontalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Signal Description" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Desription, Mode = OneWay}" HorizontalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!--<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<ItemsControl Height="36" Width="175">-->
<!--ItemsSource="{Binding DataContext.ScenarioHeaders, ElementName=LayoutRoot}">-->
<!--
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Border Width="150" >
<TextBlock Text="Description" TextAlignment="Center"/>
</Border>
<StackPanel Orientation="Horizontal">
<Border Width="50" >
<TextBlock Text="Signal2" TextAlignment="Center">
</TextBlock>
</Border>
<Border Width="50" >
<TextBlock Text="Signal1" TextAlignment="Center"/>
</Border>
</StackPanel>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding ClassSegments}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border Width="50" >
<TextBlock/>
-->
<!--Text="{Binding RegularStudentCount}" TextAlignment="Center"/>-->
<!--
</Border>
<Border Width="50" >
<TextBlock></TextBlock> -->
<!--Text="{Binding EIPStudentCount}" TextAlignment="Center"/>-->
<!--
</Border>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>-->
<DataGridCheckBoxColumn Header="Inverse" Width="Auto" Binding="{Binding Inverse, Mode = TwoWay}" >
<!--<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Inverse, Mode = OneWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>-->
</DataGridCheckBoxColumn>
<DataGridTemplateColumn Header="Period(ssp)" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding period, Mode = OneWay}" HorizontalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Timing" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Timing, Mode = OneWay}" HorizontalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</ScrollViewer>
</Grid>
Sample image of two merged datagrids
Scroll viewer is disabling virtualization. I need to have merged cells in some columns of my datagrid. Please let me know of any alternate solution to achieve the same without losing virtualization.
Virtualization only works if the grid is constrained in size.
If you place DataGrid inside ScrollViewer or StackPanel or any control that does not pass fix constraint to DataGrid it will break the virtualization in DataGrid.
it will be better if you can give me a little working example, but anyway using ScrollViewer as parent of DataGrid is not a good idea, I think it will be better to override DataGrid and have dependency property for scrollValue, and you should have independent scrollbar and bind this dependency property for both DataGrid to this scrollbar value, in this way you can scroll both DataGrids via code behind while scrollbar value changed, and you virtualization should work.
Hi. I have added code in github, please download it and try https://github.com/Ilya-Grigoryan/SynchDataGridScroll.git

How to fix an endless rendering / measure loop with WPF?

I have the following XAML code implementing a data grid with DirectionalNavigation = Cycle.
<DataGrid
KeyboardNavigation.DirectionalNavigation="Cycle"
x:Name="ToolPathGridView"
VirtualizingPanel.VirtualizationMode="Recycling"
ScrollViewer.IsDeferredScrollingEnabled="True"
CanUserAddRows="False"
SelectionUnit="FullRow"
CanUserDeleteRows="False"
SelectionMode="Single"
ItemsSource="{Binding
ToolPath.ToolPath,
Mode=OneWay,
Converter={StaticResource IndexedConverter}}"
AutoGenerateColumns="False"
SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}">
<DataGrid.Resources>
<SolidColorBrush
x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
Color="DarkGray"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
Id
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Index}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<StackPanel Orientation="Horizontal">
<TextBlock
Text="{Binding Source={StaticResource T9N},
Path=SwivelAxis}" />
</StackPanel>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Value.ToolAxes.Swivel}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<StackPanel Orientation="Horizontal">
<TextBlock
Text="{Binding Source={StaticResource T9N},
Path=ChuckAxis}" />
</StackPanel>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Value.ToolAxes.Chuck}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<StackPanel Orientation="Horizontal">
<TextBlock
Text="{Binding Source={StaticResource T9N},
Path=VerticalAxis}" />
</StackPanel>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Value.ToolAxes.Vertical}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Using the arrow keys I can move up and down the rows.
If the last row in the data-grid is selected, as above, and the down arrow is pressed then the first row in the list should become selected. However WPF goes into an endless measure / arrange loop.
I previously had custom user controls in each of the cell templates but have removed them and replaced them with plain labels just in case my controls were behaving badly.
Does anybody have an idea if this is a bug in WPF or a problem with my usage of the data grid?

Can i make UpDown Column at DataGrid?

Can i make UpDown Column at DataGrid?
I have simple DataGrid:
<DataGrid Grid.Row="0" Grid.Column="0">
<DataGrid.Columns>
<DataGridTextColumn Header="Name"> </DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
And i want to make UpDown column. Can i do that?
Thank you!
P.S. i mean something like numericUpDown counter. :up: [1], down: [0].
I think you can do something like
<DataGrid>
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=AttrName}" Height="25" Width="150" HorizontalAlignment="Left" VerticalAlignment="Top" />
<TextBlock Text="{Binding Path=AttrDisplayLabel}" Height="25" Width="Auto" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="10,0,0,0" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Height="25"
ItemsSource="{Binding Source={StaticResource cvsAttributes}}"
SelectedValuePath="AttributeID"
IsSynchronizedWithCurrentItem="False"
SelectionChanged="Selector_OnSelectionChanged"
SelectedValue="{Binding Path=AttributeId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
....
Just replace combo with your updown/spinner control. The celltemplate is your display... the celledittemplate is your edit control...(updown...etc...)
If you mean NumericUpDown column
you can have a template column and add NumericUpDown Control to the template
Look Here for creating custom numeric updown control or simply use one provided with WPFToolkit !!
Find Codeples for Toolkit Here

Categories

Resources