向下滚动后DataGrid更新

时间:2013-08-13 10:40:19

标签: wpf xaml datagrid

我在WPF(c#)中编写程序。我使用DataSet来填充DataGrid。向上/向下滚动后,DataGrid单元格会更新并更改其位置和大小!我怎么解决这个问题? 我希望DataGrid在滚动之前自行更新。

这里是例子:

滚动前:

Before

滚动后

enter image description here


这是我的XAML代码:

<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>

这是C#代码:

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;

        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为这个问题是由UI-Virtualization引起的:WPF只是填充网格的可见部分,因此它只测量这些可见记录的宽度。

可能的解决方案:

  • 禁用虚拟化(但请记住,这可能会影响性能......)
  • 手动配置列的宽度(DataGrid.AutoGenerateColumns = false)