1

要自定义ScrollBar我设置的外观:

<sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">15</sys:Double>
<sys:Double x:Key="{x:Static SystemParameters.HorizontalScrollBarHeightKey}">15</sys:Double>

在我App.xamlThemes\Generic.xaml一些自定义控件中Grid.Column定义如下:

<ColumnDefinition Width="{Binding Source={x:Static SystemParameters.VerticalScrollBarWidth}}"/> 

看起来我的自定义控件实际上不使用15as 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 中的这些参数,它看起来像预期的那样:

在此处输入图像描述

4

2 回答 2

0

我不确定我是否正确,实际上无法更改常量VerticalScrollBarWidthHorizontalScrollBarHeight(在我的情况下,宽度和高度始终为 17)但在运行时,系统使用这些键值来呈现应用程序的滚动条到你想要的宽度。因此,在我看来,最好的解决方法是在这样的地方声明一个常量:

public const double ScrollBarThickness = 12;

而不是设置这些:

<sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">12</sys:Double>
<sys:Double x:Key="{x:Static SystemParameters.HorizontalScrollBarHeightKey}">12</sys:Double>

以这种方式App.xaml设置这些值App.xaml.cs

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    Resources.Add(SystemParameters.VerticalScrollBarWidthKey, Constants.ScrollBarThickness);
    Resources.Add(SystemParameters.HorizontalScrollBarHeightKey, Constants.ScrollBarThickness);
}

并在您需要的任何地方使用该常量进行绑定。在我的情况下:我想Grid.Column在我的自定义控件中重新定义这两个定义和虚拟边框的宽度,如下所示:

<ColumnDefinition Width="{Binding Source={x:Static con:Constants.ScrollBarThickness}}"/>
<Border Grid.Column="5" Width="{Binding Source={x:Static con:Constants.ScrollBarThickness}}">

我实际上根本不需要那些合并技术,没有这些就可以了!我也很想绑定常量,App.xaml但我不知道如何为VerticalScrollBarWidthKeyor定义样式VerticalScrollBarWidthKey。什么是属性名称 (???):

<Style x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">
     <Setter Property="???" Value="{Binding Source={x:Static con:Constants.ScrollBarThickness}}"/>
</Style>

如果我想绑定App.xaml而不是App.xaml.cs

于 2020-06-02T03:52:35.630 回答
0

将资源添加到App.xaml确实有效。

不过,您引用的方式ResourceDictionary是错误的。您应该将其合并到App.xaml

<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml"
             xmlns:sys="clr-namespace:System;assembly=System.Runtime">
    <Application.Resources>
        <ResourceDictionary>
            <sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">15</sys:Double>

            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/RentManager;component/Themes/Generic.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>


    </Application.Resources>
</Application>
于 2020-06-01T13:22:22.753 回答