要自定义ScrollBar
我设置的外观:
<sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">15</sys:Double>
<sys:Double x:Key="{x:Static SystemParameters.HorizontalScrollBarHeightKey}">15</sys:Double>
在我App.xaml
的Themes\Generic.xaml
一些自定义控件中Grid.Column
定义如下:
<ColumnDefinition Width="{Binding Source={x:Static SystemParameters.VerticalScrollBarWidth}}"/>
看起来我的自定义控件实际上不使用15
as Width
。有没有办法强制自定义控件样式在Themes\Generic.xaml
使用这些值中设置App.xaml
?
编辑
这是我App.xaml
现在所拥有的:
<Application x:Class="RentManager.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:RentManager"
xmlns:sys="clr-namespace:System;assembly=System.Runtime"
xmlns:cc="clr-namespace:RentManager.CustomControl"
xmlns:con="clr-namespace:RentManager.Common"
StartupUri="Main.xaml">
<Application.Resources>
<ResourceDictionary>
<sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">12</sys:Double>
<sys:Double x:Key="{x:Static SystemParameters.HorizontalScrollBarHeightKey}">12</sys:Double>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/RentManager;component/Themes/Generic.xaml" />
<ResourceDictionary Source="pack://application:,,,/RentManager;component/Themes/OtherStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
当滚动条出现时,我遇到问题的 userControl 如下所示:
查看Receivable/Payment
列中的未对齐。这是没有滚动条的样子:
Ledger 的自定义控件是:
<Style TargetType="{x:Type local:Ledger}">
<Setter Property="Focusable" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:Ledger}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="{Binding Source={x:Static SystemParameters.VerticalScrollBarWidth}}"/>
</Grid.ColumnDefinitions>
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="Grid.Row" Value="1"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</Grid.Resources>
<Border Grid.Row="1" Grid.ColumnSpan="5" Background="AliceBlue"/>
<Separator Grid.Row="0" Grid.ColumnSpan="5"/>
<TextBlock Text="Date" Margin="20 0 0 0"/>
<TextBlock Grid.Column="1" Text="Particulars"/>
<TextBlock Grid.Column="2" HorizontalAlignment="Right">
<Run Text="Receivable"/>
<LineBreak/>
<Run Text="/ Payment"/>
</TextBlock>
<TextBlock Grid.Column="3" HorizontalAlignment="Right" Text="Receipt"/>
<TextBlock Grid.Column="4" HorizontalAlignment="Right" Text="Balance" Margin="0 0 20 0"/>
<Separator Grid.Row="2" Grid.ColumnSpan="5"/>
</Grid>
<ItemsControl Grid.Row="1"
ItemsSource="{TemplateBinding Source}"
HorizontalContentAlignment="Stretch"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling"
ScrollViewer.CanContentScroll="True">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer VerticalScrollBarVisibility="Auto" x:Name="scroll">
<ItemsPresenter/>
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid >
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Date, StringFormat='dd MMM yyyy'}" Margin="20 0 0 0"/>
<ContentControl Grid.Column="1">
<ContentControl.Style>
<Style BasedOn="{StaticResource {x:Type ContentControl}}" TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding DataContext.SelectedTab.Type, RelativeSource={RelativeSource AncestorType={x:Type local:Ledger}}}" Value="Plot">
<Setter Property="Content">
<Setter.Value>
<TextBlock TextWrapping="Wrap">
<Run Text="{Binding SpaceName}"/>
<Run Text=": "/>
<Run Text="{Binding TenantName}"/>
<Run Text=" - "/>
<Run Text="{Binding HeadName}"/>
<Run Text=" | "/>
<Run Text="{Binding Narration}" FontStyle="Italic" Foreground="Blue"/>
</TextBlock>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding DataContext.SelectedTab.Type, RelativeSource={RelativeSource AncestorType={x:Type local:Ledger}}}" Value="Space">
<Setter Property="Content">
<Setter.Value>
<TextBlock TextWrapping="Wrap">
<Run Text="{Binding PlotName}"/>
<Run Text=": "/>
<Run Text="{Binding TenantName}"/>
<Run Text=" - "/>
<Run Text="{Binding HeadName}"/>
<Run Text=" | "/>
<Run Text="{Binding Narration}" FontStyle="Italic" Foreground="Blue"/>
</TextBlock>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding DataContext.SelectedTab.Type, RelativeSource={RelativeSource AncestorType={x:Type local:Ledger}}}" Value="Tenant">
<Setter Property="Content">
<Setter.Value>
<TextBlock TextWrapping="Wrap">
<Run Text="{Binding PlotName}"/>
<Run Text=": "/>
<Run Text="{Binding SpaceName}"/>
<Run Text=" - "/>
<Run Text="{Binding HeadName}"/>
<Run Text=" | "/>
<Run Text="{Binding Narration}" FontStyle="Italic" Foreground="Blue"/>
</TextBlock>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
<local:NumReport Grid.Column="2" Text="{Binding Receivable}" />
<local:NumReport Grid.Column="3" Text="{Binding Receipt}" />
<local:NumReport Grid.Column="4" Text="{Binding Balance}" Margin="0 0 20 0"/>
<Border Grid.Column="5" Width="{Binding Source={x:Static SystemParameters.VerticalScrollBarWidth}}">
<Border.Style>
<Style TargetType="Border">
<Style.Triggers>
<DataTrigger Binding="{Binding ComputedVerticalScrollBarVisibility, ElementName=scroll}" Value="Visible">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
<Separator Grid.Row="1" Grid.ColumnSpan="5" Background="LightBlue" Margin="0 5 0 5"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Grid Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="{Binding Source={x:Static SystemParameters.VerticalScrollBarWidth}}"/>
</Grid.ColumnDefinitions>
<Border Grid.Row="1" Grid.ColumnSpan="5" Background="AliceBlue"/>
<Separator Grid.Row="0" Grid.ColumnSpan="5"/>
<TextBlock Grid.Row="1" Text="Total" FontWeight="Bold" Margin="20 0 0 0"/>
<local:NumReport Grid.Row="1" Grid.Column="2" FontWeight="Bold" Text="{Binding TotalReceivable}"/>
<local:NumReport Grid.Row="1" Grid.Column="3" FontWeight="Bold" Text="{Binding TotalReceipt}"/>
<Separator Grid.Row="2" Grid.ColumnSpan="5"/>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
没有这两行:
<sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">12</sys:Double>
<sys:Double x:Key="{x:Static SystemParameters.HorizontalScrollBarHeightKey}">12</sys:Double>
在App.xaml
它按预期工作!另一件事是在添加那些 MergedDictionaries 之后App.xaml
,现在我的Generic.xaml
.
编辑
如果我不更改 App.xaml 中的这些参数,它看起来像预期的那样: