删除/替换datagrid模板中的scrollviewer控件 [英] Remove/replace scrollviewer control in datagrid template
问题描述
我想删除或替换wpf DataGrid模板中的ScrollViewer。我想这样做的原因是因为我在我的应用程序中使用自定义滚动条,数据网格将不支持平滑滚动。但我遇到了类似的问题,当我使用树视图时失去平滑滚动功能,但我解决了它从TreeView的模板中删除滚动查看器。下面是WPF TreeView的默认模板。
<样式x:Key ={x:Type TreeView}
TargetType ={x:Type TreeView}>
< Setter Property =Background
Value ={DynamicResource {x:Static SystemColors.WindowBrushKey}}/>
< Setter Property =BorderBrush
Value ={StaticResource ListBorder}/>
< Setter Property =BorderThickness
Value =1/>
< Setter Property =Padding
Value =1/>
< Setter Property =Foreground
Value ={DynamicResource {x:Static SystemColors.ControlTextBrushKey}}/>
< Setter Property =ScrollViewer.HorizontalScrollBarVisibility
Value =Auto/>
< Setter Property =ScrollViewer.VerticalScrollBarVisibility
Value =Auto/>
< Setter Property =ScrollViewer.PanningMode
Value =Both/>
< Setter Property =Stylus.IsFlicksEnabled
Value =False/>
< Setter Property =VerticalContentAlignment
Value =Center/>
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType ={x:Type TreeView}>
< Border Name =Bd
BorderBrush ={TemplateBinding BorderBrush}
BorderThickness ={TemplateBinding BorderThickness}
SnapsToDevicePixels =true>
< ScrollViewer Name =_ tv_scrollviewer_
Background ={TemplateBinding Background}
Focusable =false
CanContentScroll =false
HorizontalScrollBarVisibility ={ TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}
VerticalScrollBarVisibility ={TemplateBinding ScrollViewer.VerticalScrollBarVisibility}
Padding ={TemplateBinding Padding}
SnapsToDevicePixels ={TemplateBinding SnapsToDevicePixels}>
< ItemsPresenter />
< / ScrollViewer>
< / Border>
< ControlTemplate.Triggers>
< Trigger Property =IsEnabled
Value =false>
< Setter TargetName =Bd
Property =Background
Value ={DynamicResource {x:Static SystemColors.ControlBrushKey}}/>
< / Trigger>
< Trigger Property =VirtualizingPanel.IsVirtualizing
Value =true>
< Setter TargetName =_ tv_scrollviewer_
Property =CanContentScroll
Value =true/>
< / Trigger>
< /ControlTemplate.Triggers>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< Style.Triggers>
< Trigger Property =VirtualizingPanel.IsVirtualizing
Value =true>
< Setter Property =ItemsPanel>
< Setter.Value>
< ItemsPanelTemplate>
< VirtualizingStackPanel />
< / ItemsPanelTemplate>
< /Setter.Value>
< / Setter>
< / Trigger>
< /Style.Triggers>
< / Style>
下面是我重新定义的模板,即滚动查看器被删除的模板所以当鼠标悬停在TreeView控件上时,我可以平滑滚动。
<样式x:Key =RoleTreeViewStyleTargetType ={x:Type TreeView} BasedOn ={StaticResource BaseStyle}>
< Setter Property =BackgroundValue ={StaticResource BackgroundVeryLightBrush}/>
< Setter Property =ForegroundValue ={StaticResource ForegroundVeryDarkBrush}/>
< Setter Property =BorderThicknessValue =0/>
< Setter Property =PaddingValue =0 5/>
< Setter Property =VerticalContentAlignmentValue =Center/>
< Setter Property =FontFamilyValue ={StaticResource LatoRegular}/>
< Setter Property =FontSizeValue ={StaticResource FontSizeLarge}/>
< Setter Property =VerticalAlignmentValue =Top/>
< Setter Property =HorizontalAlignmentValue =Left/>
< Setter Property =MarginValue =5 0 5 0/>
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType ={x:Type TreeView}>
< Border x:Name =BdBorderBrush ={TemplateBinding BorderBrush}
BorderThickness ={TemplateBinding BorderThickness}SnapsToDevicePixels =true>
< StackPanel>
< ItemsPresenter />
< / StackPanel>
< / Border>
< ControlTemplate.Triggers>
< Trigger Property =IsEnabledValue =false>
< Setter Property =Panel.BackgroundTargetName =Bd
Value ={StaticResource WordVeryVeryLightBlueBrush}/>
< / Trigger>
< /ControlTemplate.Triggers>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< Style.Triggers>
< Trigger Property =VirtualizingPanel.IsVirtualizingValue =true>
< Setter Property =ItemsPanel>
< Setter.Value>
< ItemsPanelTemplate>
< VirtualizingStackPanel />
< / ItemsPanelTemplate>
< /Setter.Value>
< / Setter>
< / Trigger>
< /Style.Triggers>
< / Style>
所以在第二个模板中,我用StackPanel替换了ScrollViewer。 br />
所以我的问题是如何在不影响DataGrid显示和功能的情况下使用下面的DataGrid模板做类似的事情?我的主要目标是在模板中删除/替换ScrollViewer,而不会影响DataGrid的功能。
DataGrid默认模板
< Style x:Key ={ x:键入DataGrid}TargetType ={x:Type DataGrid}>
< Setter Property =Background
Value ={DynamicResource {x:Static SystemColors.ControlBrushKey}}/>
< Setter Property =Foreground
Value ={DynamicResource {x:Static SystemColors.ControlTextBrushKey}}/>
< Setter Property =BorderBrushValue =#FF688CAF/>
< Setter Property =BorderThicknessValue =1/>
< Setter Property =RowDetailsVisibilityModeValue =VisibleWhenSelected/>
< Setter Property =ScrollViewer.CanContentScroll
Value =true/>
< Setter Property =ScrollViewer.PanningMode
Value =Both/>
< Setter Property =Stylus.IsFlicksEnabled
Value =False/>
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType ={x:Type DataGrid}>
< Border Background ={TemplateBinding Background}
BorderBrush ={TemplateBinding BorderBrush}
BorderThickness ={TemplateBinding BorderThickness}
SnapsToDevicePixels =True
Padding ={TemplateBinding Padding}>
< ScrollViewer Focusable =false
Name =DG_ScrollViewer>
< ScrollViewer.Template>
< ControlTemplate TargetType ={x:Type ScrollViewer}>
< Grid>
< Grid.RowDefinitions>
< RowDefinition Height =Auto/>
< RowDefinition Height =*/>
< RowDefinition Height =Auto/>
< /Grid.RowDefinitions>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =Auto/>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =Auto/>
< /Grid.ColumnDefinitions>
< Button Command ={x:Static DataGrid.SelectAllCommand}
Width ={Binding RelativeSource = {RelativeSource AncestorType = {x:Type DataGrid}},Path = CellsPanelHorizontalOffset}
Style ={DynamicResource {ComponentResourceKey TypeInTargetAssembly = {x:Type DataGrid},ResourceId = DataGridSelectAllButtonStyle}}
Focusable =false
Visibility ={Binding RelativeSource = {RelativeSource AncestorType = {x:类型DataGrid}},Path = HeadersVisibility,Converter = {x:Static DataGrid.HeadersVisibilityConverter},ConverterParameter = {x:Static DataGridHeadersVisibility.All}}/>
< DataGridColumnHeadersPresenter Grid.Column =1
Name =PART_ColumnHeadersPresenter
Visibility ={Binding RelativeSource = {RelativeSource AncestorType = {x:Type DataGrid}},Path = HeadersVisibility, Converter = {x:Static DataGrid.HeadersVisibilityConverter},ConverterParameter = {x:Static DataGridHeadersVisibility.Column}}/>
< ScrollContentPresenter x:Name =PART_ScrollContentPresenterGrid.Row =1Grid.ColumnSpan =2CanContentScroll ={TemplateBinding CanContentScroll}/>
< ScrollBar Grid.Row =1
Grid.Column =2
Name =PART_VerticalScrollBar
Orientation =Vertical
Maximum ={TemplateBinding ScrollableHeight}
ViewportSize ={TemplateBinding ViewportHeight}
Value ={Binding Path = VerticalOffset,RelativeSource = {RelativeSource TemplatedParent},Mode = OneWay}
Visibility ={TemplateBinding ComputedVerticalScrollBarVisibility}/>
< Grid Grid.Row =2Grid.Column =1>
< Grid.ColumnDefinitions>
< ColumnDefinition Width ={Binding RelativeSource = {RelativeSource AncestorType = {x:Type DataGrid}},Path = NonFrozenColumnsViewportHorizontalOffset}/>
< ColumnDefinition Width =*/>
< /Grid.ColumnDefinitions>
< ScrollBar Grid.Column =1
Name =PART_HorizontalScrollBar
Orientation =Horizontal
Maximum ={TemplateBinding ScrollableWidth}
ViewportSize = {TemplateBinding ViewportWidth}
Value ={Binding Path = HorizontalOffset,RelativeSource = {RelativeSource TemplatedParent},Mode = OneWay}
Visibility ={TemplateBinding ComputedHorizontalScrollBarVisibility}/>
< / Grid>
< / Grid>
< / ControlTemplate>
< /ScrollViewer.Template>
< ItemsPresenter SnapsToDevicePixels ={TemplateBinding SnapsToDevicePixels}/>
< / ScrollViewer>
< / Border>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< Style.Triggers>
< MultiTrigger>
< MultiTrigger.Conditions>
< Condition Property =IsGroupingValue =true/>
< Condition Property =VirtualizingPanel.IsVirtualizingWhenGroupingValue =false/>
< /MultiTrigger.Conditions>
< Setter Property =ScrollViewer.CanContentScrollValue =false/>
< / MultiTrigger>
< /Style.Triggers>
< / Style>
我们将非常感谢任何帮助或提示。
我的尝试:
我试图用StackPanel,ContentPresenter等替换它,但这些控件都不支持模板,因为我收到错误消息,例如类型模板不是识别或访问当我做< stackpanel.template>。
>< pre>< pre lang =c# >< /预>< /预>
<代码><代码><代码><代码><代码><代码> <代码><代码>
I want to remove or replace the ScrollViewer found in the template of a wpf DataGrid. The reason i want to do this is because the datagrid won't support smooth scrolling as a result of me using a custom scrollbar in my app. But i have faced this similar problem of loosing smooth scrolling capability when i used a treeview but i solved it be removing the scrollviewer from the TreeView's template. Below is the default template for a WPF TreeView.
<Style x:Key="{x:Type TreeView}" TargetType="{x:Type TreeView}"> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> <Setter Property="BorderBrush" Value="{StaticResource ListBorder}"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="Padding" Value="1"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/> <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> <Setter Property="ScrollViewer.PanningMode" Value="Both"/> <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TreeView}"> <Border Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true"> <ScrollViewer Name="_tv_scrollviewer_" Background="{TemplateBinding Background}" Focusable="false" CanContentScroll="false" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"> <ItemsPresenter/> </ScrollViewer> </Border> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="false"> <Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> </Trigger> <Trigger Property="VirtualizingPanel.IsVirtualizing" Value="true"> <Setter TargetName="_tv_scrollviewer_" Property="CanContentScroll" Value="true"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="VirtualizingPanel.IsVirtualizing" Value="true"> <Setter Property="ItemsPanel"> <Setter.Value> <ItemsPanelTemplate> <VirtualizingStackPanel/> </ItemsPanelTemplate> </Setter.Value> </Setter> </Trigger> </Style.Triggers> </Style>
Below is the template i redefined, that is, the template in which the scrollviewer is removed so i can have smooth scrolling when the mouse is over the TreeView control.
<Style x:Key="RoleTreeViewStyle" TargetType="{x:Type TreeView}" BasedOn="{StaticResource BaseStyle}"> <Setter Property="Background" Value="{StaticResource BackgroundVeryLightBrush}"/> <Setter Property="Foreground" Value="{StaticResource ForegroundVeryDarkBrush}"/> <Setter Property="BorderThickness" Value="0"/> <Setter Property="Padding" Value="0 5"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="FontFamily" Value="{StaticResource LatoRegular}" /> <Setter Property="FontSize" Value="{StaticResource FontSizeLarge}" /> <Setter Property="VerticalAlignment" Value="Top" /> <Setter Property="HorizontalAlignment" Value="Left" /> <Setter Property="Margin" Value="5 0 5 0" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TreeView}"> <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true"> <StackPanel> <ItemsPresenter/> </StackPanel> </Border> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Panel.Background" TargetName="Bd" Value="{StaticResource WordVeryVeryLightBlueBrush}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="VirtualizingPanel.IsVirtualizing" Value="true"> <Setter Property="ItemsPanel"> <Setter.Value> <ItemsPanelTemplate> <VirtualizingStackPanel/> </ItemsPanelTemplate> </Setter.Value> </Setter> </Trigger> </Style.Triggers> </Style>
So in the second template, i replaced the ScrollViewer with a StackPanel.
So my question is how can i do something similar with the DataGrid template below without affecting the DataGrid display and functionality? My main goal is to remove/replace the ScrollViewer in the template without affecting the DataGrid's funtionality.
DataGrid default template
<Style x:Key="{x:Type DataGrid}" TargetType="{x:Type DataGrid}"> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> <Setter Property="BorderBrush" Value="#FF688CAF" /> <Setter Property="BorderThickness" Value="1" /> <Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected" /> <Setter Property="ScrollViewer.CanContentScroll" Value="true"/> <Setter Property="ScrollViewer.PanningMode" Value="Both"/> <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type DataGrid}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True" Padding="{TemplateBinding Padding}"> <ScrollViewer Focusable="false" Name="DG_ScrollViewer"> <ScrollViewer.Template> <ControlTemplate TargetType="{x:Type ScrollViewer}"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Button Command="{x:Static DataGrid.SelectAllCommand}" Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=CellsPanelHorizontalOffset}" Style="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type DataGrid}, ResourceId=DataGridSelectAllButtonStyle}}" Focusable="false" Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=HeadersVisibility, Converter={x:Static DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static DataGridHeadersVisibility.All}}" /> <DataGridColumnHeadersPresenter Grid.Column="1" Name="PART_ColumnHeadersPresenter" Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=HeadersVisibility, Converter={x:Static DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static DataGridHeadersVisibility.Column}}"/> <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" Grid.Row="1" Grid.ColumnSpan="2" CanContentScroll="{TemplateBinding CanContentScroll}" /> <ScrollBar Grid.Row="1" Grid.Column="2" Name="PART_VerticalScrollBar" Orientation="Vertical" Maximum="{TemplateBinding ScrollableHeight}" ViewportSize="{TemplateBinding ViewportHeight}" Value="{Binding Path=VerticalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/> <Grid Grid.Row="2" Grid.Column="1"> <Grid.ColumnDefinitions> <ColumnDefinition Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=NonFrozenColumnsViewportHorizontalOffset}"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <ScrollBar Grid.Column="1" Name="PART_HorizontalScrollBar" Orientation="Horizontal" Maximum="{TemplateBinding ScrollableWidth}" ViewportSize="{TemplateBinding ViewportWidth}" Value="{Binding Path=HorizontalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/> </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>
Any help or hint will be greatly appreciated.
What I have tried:
I have tried to replaced it with a StackPanel, ContentPresenter etc but none of these controls support Templates as i get error message such as "The type Template is not recognized or accessible" when i do <stackpanel.template>.
><pre><pre lang="c#"></pre></pre>
<code><code><code><code><code><code><code><code>
这篇关于删除/替换datagrid模板中的scrollviewer控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!