I write a program in WPF(c#). I use DataSet to populate DataGrid. DataGrid cells update and change their position and their size after scrolling up/down! how can I solve this problem? I want to DataGrid updates itself before scrolling.
here is example:
Before scrolling:
After scrolling:
Here is my XAML code:
<Window x:Class="RFID.CareerWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CareerWindow" Height="611" Width="459" Background="DarkGray">
<Window.Resources>
<Style x:Key="GroupBoxStyle" TargetType="{x:Type GroupBox}">
<Setter Property="FontFamily" Value="2 badr" />
<Setter Property="FontSize" Value="20" />
<Setter Property="FlowDirection" Value="RightToLeft"/>
</Style>
</Window.Resources>
<Viewbox VerticalAlignment="Top">
<Grid Width="428" Height="548">
<Grid.Resources>
<Style x:Key="GroupBoxStyle" TargetType="{x:Type GroupBox}">
<Setter Property="FontFamily" Value="2 badr" />
<Setter Property="FontSize" Value="20" />
<Setter Property="FlowDirection" Value="RightToLeft"></Setter>
</Style>
<Style x:Key="DataGridCellStyle" TargetType="DataGridCell">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</Grid.Resources>
<GroupBox Header="نوع عملیات" Margin="10,200,10,0" VerticalAlignment="Top" Height="110" Style="{StaticResource GroupBoxStyle}">
<Canvas>
<RadioButton Content="اضافه کردن" Canvas.Left="10" Canvas.Top="11" IsChecked="True"/>
<RadioButton Content="بروز رسانی" Canvas.Left="161" Canvas.Top="11"/>
<RadioButton Content="حذف کردن" Canvas.Left="305" Canvas.Top="10"/>
</Canvas>
</GroupBox>
<GroupBox Header="" Margin="10,433,10,0" VerticalAlignment="Top" Height="105" Style="{StaticResource GroupBoxStyle}">
<Canvas>
<Button x:Name="btnCancel" Content="بازگشت" FontFamily="2 badr" FontSize="20" FlowDirection="LeftToRight" Click="btnApply_Click" Height="54" Canvas.Left="212" Width="139" Canvas.Top="-3"/>
<Button x:Name="btnApply" Content="ذخیره" FontFamily="2 badr" FontSize="20" FlowDirection="LeftToRight" Click="btnApply_Click" Height="54" Canvas.Left="63" Width="139" Canvas.Top="-3"/>
</Canvas>
</GroupBox>
<GroupBox Header="اطلاعات شغل" Margin="10,315,10,0" VerticalAlignment="Top" Height="110" Style="{StaticResource ResourceKey=GroupBoxStyle}">
<Canvas>
<RadioButton Content="اضافه کردن" Canvas.Left="10" Canvas.Top="11" IsChecked="True" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<RadioButton Content="بروز رسانی" Canvas.Left="161" Canvas.Top="11" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<RadioButton Content="حذف کردن" Canvas.Left="305" Canvas.Top="10" HorizontalAlignment="Center" VerticalAlignment="Center"/>
c</Canvas>
</GroupBox>
<DataGrid x:Name="dg1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="185" Width="408" FlowDirection="RightToLeft" FontFamily="2 badr" FontSize="20" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" AlternatingRowBackground="DarkGoldenrod">
</DataGrid>
</Grid>
</Viewbox>
</Window>
and here is C# code:
namespace RFID
{
/// <summary>
/// Interaction logic for CareerWindow.xaml
/// </summary>
public partial class CareerWindow : Window
{
public CareerWindow()
{
InitializeComponent();
loadAll();
}
private void loadAll()
{
DBConnect con = new DBConnect();
dg1.ItemsSource = con.getTopCustomerInScore(0).Tables[0].DefaultView;
}
}
}
I think this problem is caused by the UI-Virtualization: WPF just populates the visible part of the Grid, therefore it measures only the width of these visible records.
Possible solutions: