【问题标题】:WPF Border column span inside of grid causes UI freeze, why?网格内的 WPF 边框列跨度导致 UI 冻结,为什么?
【发布时间】:2017-07-09 14:44:13
【问题描述】:

由于边框跨越两列,我遇到了数据绑定缓慢的问题,我不知道为什么。如果我将边框更改为仅位于第一列,则数据网格数据绑定几乎是即时的。请在下面查看我的代码。

这与设置为 * 的网格中的第二列宽度有关,因为当我将其更改为 Auto 时,它会正常工作,但我希望第二个数据网格填充剩余的空白空间(我m 在另一个 View 的 TabControl 中使用这个 UserControl)

<UserControl>
<Grid Background="Transparent">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Border Grid.Row="0" 
            Grid.Column="0"
            Grid.ColumnSpan="2"
            Background="DarkGray">
        <StackPanel Orientation="Horizontal"
                    HorizontalAlignment="Left">
            <Button x:Name="LoadDataBttn" 
                    Content="Load Data"
                    Width="125"
                    Height="Auto"
                    Margin="1,0"
                    Command="{Binding LoadDataCommand}"/>
            <Button Content="Clear Data"
                    Width="125"
                    Height="Auto"
                    Margin="1,0"/>
            <Button x:Name="ExportToXMLBttn" 
                    Content="Export To XML"
                    Width="125"
                    Height="Auto"
                    Margin="1,0"/>
        </StackPanel>
    </Border>
    <Grid Grid.Row="1"
          Grid.Column="0"
          Grid.ColumnSpan="1">
        <DataGrid x:Name="DataGrid1"
                  ItemsSource="{Binding ICollectionProperty}"
                  SelectedItem="{Binding SelectedItem}"
                  AutoGenerateColumns="True"
                  CanUserAddRows="False"
                  CanUserDeleteRows="False"
                  Background="Transparent">

        </DataGrid>
    </Grid>
    <Grid Grid.Row="1"
          Grid.Column="1">
        <DataGrid ItemsSource="{Binding ICollectionProperty2}}"
                  AutoGenerateColumns="True"
                  CanUserAddRows="False"
                  CanUserDeleteRows="False"
                  Background="Transparent"
                  Style="{StaticResource AzureDataGrid}">
        </DataGrid>
    </Grid>
</Grid>

【问题讨论】:

    标签: wpf xaml datagrid wpf-controls


    【解决方案1】:

    我一直讨厌“列跨度”属性,但有时您只需要使用它。 在我看来,您应该将您的内容划分为更多的网格,以便清晰地分离元素。

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition />
        </Grid.RowDefinitions>
    
        <Border Grid.Row="1">...</Border>
    
        <Grid Grid.Row="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
    
            <DataGrid Grid.Column="0">...
            <DataGrid Grid.Column="1">...
        </Grid>
    </Grid>
    

    也许列跨度是问题...我不知道。试试吧。

    【讨论】:

    • 感谢您的评论。这就是我最终要做的事情,但我只是觉得奇怪的是,columnspan 加上 * 的网格列定义会使数据交互变得如此缓慢。就像边框/数据网格将永远在视觉元素之外继续存在
    猜你喜欢
    • 1970-01-01
    • 2013-12-15
    • 2012-08-02
    • 1970-01-01
    • 2010-12-25
    • 2015-01-16
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    相关资源
    最近更新 更多