You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Telerik UI for WPF:虚拟化RadGridView运行时动态修改所有行高

解决虚拟化RadGridView运行时修改所有行高的问题
  • 直接使用RadGridView的RowHeight属性(推荐)
    这是最简洁有效的方式,虚拟化模式下,所有行(包括后续加载的虚拟化行)都会自动应用这个高度值,无需手动遍历已渲染行。点击按钮时的代码示例:

    private void ChangeRowHeightButton_Click(object sender, RoutedEventArgs e)
    {
        grid.RowHeight = 50;
    }
    
  • 通过动态修改行样式实现
    如果需要更灵活的样式控制,可以定义全局行样式,运行时修改样式中的Height setter:
    先在XAML中定义行样式:

    <Window.Resources>
        <Style x:Key="CustomGridViewRowStyle" TargetType="telerik:GridViewRow">
            <Setter Property="Height" Value="30"/>
        </Style>
    </Window.Resources>
    
    <telerik:RadGridView x:Name="grid" RowStyle="{StaticResource CustomGridViewRowStyle}"/>
    

    然后在按钮点击事件中更新样式:

    private void ChangeRowHeightButton_Click(object sender, RoutedEventArgs e)
    {
        var rowStyle = (Style)FindResource("CustomGridViewRowStyle");
        var heightSetter = rowStyle.Setters.OfType<Setter>()
            .FirstOrDefault(s => s.Property == FrameworkElement.HeightProperty);
        
        if (heightSetter != null)
        {
            heightSetter.Value = 50;
            // 触发样式更新
            grid.RowStyle = null;
            grid.RowStyle = (Style)FindResource("CustomGridViewRowStyle");
        }
    }
    

为什么之前的方法无效?

  • 遍历ChildrenOfType<GridViewRow>只能获取当前可视化树中已渲染的行(即当前可见的虚拟化行),未加载的行还未创建,所以修改无法覆盖所有行。
  • 若之前使用StyleSelector无效,大概率是误用了CellStyleSelector而非RowStyleSelector,但针对所有行统一高度的场景,RowHeight属性或全局行样式更高效。

内容的提问来源于stack exchange,提问作者SuvatosLabo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 18:05:19