简体   繁体   English

带有 ControlTemplate 和 ContentPresenter 的自定义 DataGrid 控件

[英]Custom DataGrid control with ControlTemplate and ContentPresenter

I have a custom DataGrid (I extended it and included a DependencyProperty Label ), I am using the DataGrid and wanted to add Label control using ControlTemplate and ContentPresenter .我有一个自定义DataGrid (我对其进行了扩展并包含了一个DependencyProperty Label ),我正在使用DataGrid并希望使用ControlTemplateContentPresenter添加Label控件。 In the ContentTemplate the DependencyProperty Label works and displays as it should, but the ContentPresenter doesn't work or display any of the DataGrid control at all.ContentTemplateDependencyProperty Label正常工作并显示,但ContentPresenter根本不工作或显示任何DataGrid控件。 I tried it with ItemsPresenter and it shows the rows, I was wondering if there is a way to display the DataGrid using ContentPresenter in this manner?我用ItemsPresenter尝试,它显示了行,我想知道是否有办法以这种方式使用ContentPresenter显示DataGrid what's the right approach to doing this?这样做的正确方法是什么?

MyUserControl.xaml我的用户控件.xaml

<UserControl
    x:Class="MyNamespace.UI.MyUserControl"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:e="clr-namespace:MyNamespace.UI"
     mc:Ignorable="d" 
     d:DesignHeight="350" d:DesignWidth="400">
    <UserControl.Resources>
        <Style TargetType="{x:Type e:DataGrid}"  BasedOn="{StaticResource {x:Type DataGrid}}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type e:DataGrid}">
                        <StackPanel>
                            <Label Content="{Binding Label, RelativeSource={RelativeSource TemplatedParent}}" ContentStringFormat="{}{0}: " />
                            <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <WrapPanel  x:Name="LayoutRoot" Width="900" HorizontalAlignment="Stretch" Margin="12" VerticalAlignment="Stretch">
        <e:DataGrid Label="My Label 1"     ItemsSource="{Binding Source={StaticResource MySource1}}"/>
        <e:DataGrid Label="My Label 2"     ItemsSource="{Binding Source={StaticResource MySource2}}"/>
    </WrapPanel>
</UserControl>

DataGrid.cs数据网格.cs

namespace MyNamespace.UI
{
    public class DataGrid : System.Windows.Controls.DataGrid
    {
        public string Label
        {
            get { return (string)GetValue(LabelProperty); }
            set { SetValue(LabelProperty, value); }
        }

        public static readonly DependencyProperty LabelProperty =
            DependencyProperty.Register("Label", typeof(string), typeof(DataGrid), new UIPropertyMetadata(""));

        public DataGrid()
        {}
    }
}

The ControlTemplate defines the appearance of the entire control. ControlTemplate定义了整个控件的外观。 So you can't simply put a <ContentPresenter /> in your template and expect a DataGrid to show up.因此,您不能简单地将<ContentPresenter />放在模板中并期望显示DataGrid Also, you cannot inherit only part of a ControlTemplate :此外,您不能仅继承ControlTemplate一部分:

WPF: Is there a way to override part of a ControlTemplate without redefining the whole style? WPF:有没有办法在不重新定义整个样式的情况下覆盖 ControlTemplate 的一部分?

You could copy the entire default template and add your Label to it though:您可以复制整个默认模板并将您的Label添加到其中:

<Style TargetType="{x:Type e:DataGrid}"  BasedOn="{StaticResource {x:Type DataGrid}}">
    <Setter Property="Template">
        <Setter.Value>
            <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="Auto"/>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <Label Content="{Binding Label, RelativeSource={RelativeSource TemplatedParent}}" ContentStringFormat="{}{0}: " />
                                    <Button Grid.Row="1" 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.Row="1" 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="2"/>
                                    <ScrollBar x:Name="PART_VerticalScrollBar" Grid.Column="2" Maximum="{TemplateBinding ScrollableHeight}" Orientation="Vertical" Grid.Row="2" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
                                    <Grid Grid.Column="1" Grid.Row="3">
                                        <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>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsGrouping" Value="true"/>
                <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/>
            </MultiTrigger.Conditions>
            <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
        </MultiTrigger>
    </Style.Triggers>
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用ContentPresenter和ItemsPresenter进行自定义控件 - Custom control with both ContentPresenter and ItemsPresenter 在自定义控件中交换ContentPresenter内容 - Swapping ContentPresenter content in custom control 将 ContentPresenter 用于自定义控件 (Thumb) - Using a ContentPresenter for a custom control (Thumb) ContentPresenter不以样式显示自定义控件 - ContentPresenter not displaying Custom Control in Style NullReferenceException,带有自定义UserControl中ContentPresenter中的引用控件 - NullReferenceException with referenced Control in ContentPresenter from custom UserControl 使用ControlTemplate在用户控件上绑定自定义依赖项属性 - Binding custom Dependency Property on User Control with ControlTemplate 将 ControlTemplate 子项绑定到自定义控件上的 DependencyProperty 不起作用 - Binding ControlTemplate child to DependencyProperty on a custom control not working 在RowGrid-&gt; Template-&gt; ControlTemplate-&gt; DataGrid的边框内设置控件的属性 - Set the property of a control inside the RowStyle ->Template -> ControlTemplate -> Border of the DataGrid ContentPresenter 未显示 ControlTemplate Xamarin Forms 内的内容 - ContentPresenter not showing content inside ControlTemplate Xamarin Forms 如何使用ContentPresenter在自定义控件中显示BitmapImage? - How can I use a ContentPresenter to display a BitmapImage in a custom control?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM