代码之家  ›  专栏  ›  技术社区  ›  Rachel

在哪里可以找到WPF数据网格的XAML模板?[关闭]

  •  8
  • Rachel  · 技术社区  · 14 年前

    我想修改微软WPF DATAGRID(4版)的模板,但我不确定现有模板的外观。有人知道它是什么或在哪里能找到它吗?

    3 回复  |  直到 14 年前
        1
  •  7
  •   Wonko the Sane    7 年前

    我建议加入Blend(如果你有)并提取模板。否则,可以使用XAMLWriter获取XAML。

    这个 unfortunately named page (我不认为这是真的“孩子的东西”,是吗?)有一个做这两件事的例子。

    万一页面真的消失了(2009年在后视镜里还挺远的):

    // Get the template from the control 
    ControlTemplate template = ctl.Template;
    
    // We want our xaml of be properly indented, ohterwise
    // we would not be able to indent them.
    XmlWriterSettings xmlSettings = new XmlWriterSettings();
    xmlSettings.Indent = true;
    
    // Make the string builder
    StringBuilder sb = new StringBuilder();
    XmlWriter writer = XmlWriter.Create(sb, xmlSettings);
    XamlWriter.Save(template, writer);
    
    
    // Now the sb.ToString() should give us the template
    

    全部归功于沙夫卡特·艾哈迈德。

        2
  •  6
  •   ihatemash    14 年前

    下面是我从混合物4中得到的:

    <ControlTemplate TargetType="{x:Type DataGrid}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                        <ScrollViewer x:Name="DG_ScrollViewer" Focusable="false">
                            <ScrollViewer.Template>
                                <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>
                                        <Button Command="{x:Static DataGrid.SelectAllCommand}" Focusable="false" Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.All}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                        <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                        <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" Grid.ColumnSpan="2" Grid.Row="1"/>
                                        <ScrollBar x:Name="PART_VerticalScrollBar" Grid.Column="2" Maximum="{TemplateBinding ScrollableHeight}" Orientation="Vertical" Grid.Row="1" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
                                        <Grid Grid.Column="1" Grid.Row="2">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Orientation="Horizontal" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
                                        </Grid>
                                    </Grid>
                                </ControlTemplate>
                            </ScrollViewer.Template>
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
    
        3
  •  4
  •   David    14 年前

    msdn上有两个我经常使用的模板/样式列表:

    http://msdn.microsoft.com/fr-fr/library/aa970773.aspx

    http://msdn.microsoft.com/en-us/library/aa970773.aspx

    不幸的是,他们没有像广告上说的那样。。。

    就你而言,我推荐这个(从第二个链接获得): http://msdn.microsoft.com/en-us/library/ff506248.aspx

    最近帮了我很多忙!