简体   繁体   English

Generic.xaml 不使用 App.xaml 中设置的 SystemParameters

[英]Generic.xaml doesn't use SystemParameters set in App.xaml

To customize the look of ScrollBar I've set these:为了自定义ScrollBar的外观,我设置了这些:

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

in App.xaml and in the Themes\Generic.xaml some of my Custom Controls have Grid.Column defined like this:App.xamlThemes\Generic.xaml Generic.xaml 中,我的一些自定义控件的Grid.Column定义如下:

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

Looks like my Custom Controls actually don't use 15 as Width .看起来我的自定义控件实际上不使用15作为Width Is there any way to force Custom Controls Styles defined in Themes\Generic.xaml use these values set in App.xaml ?有没有办法强制在Themes\Generic.xaml Generic.xaml 中定义的自定义控件 Styles 使用App.xaml中设置的这些值?

EDIT编辑

Here's what I've in App.xaml now:这是我现在在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>

The userControl on which I'm having issue looks like this when scrollbar appears:当滚动条出现时,我遇到问题的 userControl 如下所示:

在此处输入图像描述

Look at the Misalignment in the Receivable/Payment column.查看Receivable/Payment列中的错位。 Here's how it looks without scrollbar:这是没有滚动条的样子:

在此处输入图像描述

The custom control for Ledger is: 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>

Without these two lines:没有这两行:

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

in App.xaml it works as expected!App.xaml中它按预期工作! Another thing is after Adding those MergedDictionaries in App.xaml , now I've red squiggles all over my Generic.xaml .另一件事是在App.xaml添加那些 MergedDictionaries 之后,现在我的 Generic.xaml 上到处都是红色Generic.xaml

EDIT编辑

If I don't change those parameters in App.xaml, it looks as expected:如果我不更改 App.xaml 中的这些参数,它看起来像预期的那样:

在此处输入图像描述

Adding the resource to App.xaml does work.将资源添加到App.xaml确实有效。

Your way of referencing the ResourceDictionary is wrong though.不过,您引用ResourceDictionary的方式是错误的。 You should merge it into App.xaml :您应该将其合并到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>

I'm not sure whether I'm right or not, one actually can't change the constant VerticalScrollBarWidth or HorizontalScrollBarHeight (in my case Width and Height are always 17) BUT at run time these key values are used by the System to render application's scrollbar according to your desired width.我不确定我是否正确,实际上无法更改常量VerticalScrollBarWidthHorizontalScrollBarHeight (在我的情况下 Width 和 Height 始终为 17)但在运行时系统使用这些键值来呈现应用程序的根据您想要的宽度滚动条。 So to get around the best way in my opinion is to declare a constant somewhere like this:因此,在我看来,最好的解决方法是在这样的地方声明一个常量:

public const double ScrollBarThickness = 12;

and instead of setting these:而不是设置这些:

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

in App.xaml set those values in App.xaml.cs in this way: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);
}

and use that Constant in Binding wherever you need.并在您需要的任何地方使用该常量进行绑定。 In my case: I'd to redefine those two Grid.Column definitions and the dummy Border's Width in my Custom Control like this:在我的情况下:我想在我的自定义控件中重新定义这两个Grid.Column定义和虚拟边框的宽度,如下所示:

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

I actually don't need those Merging techniques at all, it works just fine without all those!我实际上根本不需要那些合并技术,没有这些就可以了! I'd love to Bind the Constant in App.xaml as well BUT I do not know how to define Style for VerticalScrollBarWidthKey or VerticalScrollBarWidthKey .我也很想在App.xaml中绑定常量,但我不知道如何为VerticalScrollBarWidthKeyVerticalScrollBarWidthKey定义样式。 What would be the Property name (???):什么是属性名称 (???):

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

if I wanted to Bind in App.xaml instead of App.xaml.cs ?如果我想在App.xaml而不是App.xaml.cs中绑定?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM