DataGrid.FrozenColumnCount 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置非滚动列的数目。
public:
property int FrozenColumnCount { int get(); void set(int value); };
public int FrozenColumnCount { get; set; }
member this.FrozenColumnCount : int with get, set
Public Property FrozenColumnCount As Integer
属性值
无法滚动的列数。 注册的默认值为 0。 有关可以影响值的因素的更多信息,请参见 DependencyProperty。
示例
以下示例演示如何在从 中选择“冻结列”时使 ContextMenu列冻结。 列将移动到左侧,以便包含在冻结列中。
<!--Defines the handlers for when the FreezeColumnCommand is executed-->
<Window.CommandBindings>
<CommandBinding Command="{x:Static local:Window1.FreezeColumnCommand}" Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<!--Defines the context menu for the ColumnHeaders and attaches the FreezeColumnCommand-->
<Window.Resources>
<ContextMenu x:Key="ColumnHeaderMenu" IsEnabled="True" >
<MenuItem Header="Freeze Column" IsEnabled="True" Command="{x:Static local:Window1.FreezeColumnCommand}"
CommandTarget="{Binding RelativeSource={RelativeSource AncestorType=Popup}, Path=PlacementTarget}"
CommandParameter="{Binding ElementName=DG1, Path=CurrentColumn.DisplayIndex}" />
</ContextMenu>
</Window.Resources>
<Grid>
<!--Creates a new DataGrid with a context menu for the column headers-->
<DataGrid Name="DG1" ItemsSource="{Binding}" >
<DataGrid.ColumnHeaderStyle >
<Style TargetType="DataGridColumnHeader">
<Setter Property="ContextMenu" Value="{StaticResource ColumnHeaderMenu}" />
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
</Grid>
public partial class Window1 : Window
{
public static RoutedUICommand FreezeColumnCommand = new RoutedUICommand();
public Window1()
{
InitializeComponent();
//GetData connects to the database and returns the data in a table.
AdventureWorksLT2008DataSet.SalesOrderDetailDataTable dt = GetData();
DG1.DataContext = dt;
}
Class Window1
Public Shared FreezeColumnCommand As New RoutedUICommand()
Public Sub New()
InitializeComponent()
'GetData connects to the database and returns the data in a table.
Dim dt As AdventureWorksLT2008DataSet.SalesOrderDetailDataTable = GetData()
DG1.DataContext = dt
End Sub
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
//Get the column header that started the command and move that column left to freeze it.
System.Windows.Controls.Primitives.DataGridColumnHeader header = (System.Windows.Controls.Primitives.DataGridColumnHeader)e.OriginalSource;
if (header.Column.IsFrozen ==true)
{
return;
}
else
{
header.Column.DisplayIndex = DG1.FrozenColumnCount;
DG1.FrozenColumnCount++;
}
}
}
Private Sub CommandBinding_Executed(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
'Get the column header that started the command and move that column left to freeze it.
Dim header As System.Windows.Controls.Primitives.DataGridColumnHeader = DirectCast(e.OriginalSource, System.Windows.Controls.Primitives.DataGridColumnHeader)
If header.Column.IsFrozen = True Then
Exit Sub
Else
header.Column.DisplayIndex = DG1.FrozenColumnCount
DG1.FrozenColumnCount += 1
End If
End Sub
注解
冻结列是始终显示且无法滚动到可见性的列。 冻结列始终是显示顺序最左侧的列。 不能将冻结列拖动到未冻结列组中,也不能将未冻结的列拖动到冻结列组中。
若要冻结列,请设置 FrozenColumnCount 属性。 数字 FrozenColumnCount 指定的最左侧列将冻结。 例如,如果将 设置为 FrozenColumnCount 2,则显示中的左两列将冻结。 若要确定列是否已冻结,请检查 IsFrozen 上的 DataGridColumn属性。