简体   繁体   English

在设计时未应用WPF控件样式

[英]WPF Control style is not applied at design time

In my project I'm using Extended WPF Toolkit . 在我的项目中,我使用Extended WPF Toolkit I had to override the default style for WatermarkTextBox . 我必须重写WatermarkTextBox的默认样式。 On first sight it was ok - application launches and style is being applied, but when I look at designer - all controls except WatermarkTextBox have applied designs and WatermarkTextBox has not.] 乍一看还可以-应用程序启动并应用样式,但是当我看设计师时-除WatermarkTextBox之外的所有控件都应用了设计,而WatermarkTextBox没有。

My resource dictionary file: 我的资源字典文件:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
                xmlns:wpf="clr-namespace:System.Windows;assembly=PresentationFramework" 
                xmlns:system="clr-namespace:System;assembly=mscorlib"
                xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit">

And style (all static resources are defined in the same file at beginning): 和样式(所有静态资源在开始时都在同一个文件中定义):

<Style x:Key="{x:Type xctk:WatermarkTextBox}" TargetType="{x:Type xctk:WatermarkTextBox}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="MinWidth" Value="50" />
    <Setter Property="MinHeight" Value="34" />
    <Setter Property="Height" Value="34" />
    <Setter Property="AllowDrop" Value="True" />
    <Setter Property="Foreground" Value="{StaticResource DarkGrayBrush}" />
    <Setter Property="BorderBrush" Value="{StaticResource GrayBrush}" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Background" Value="{StaticResource WhiteBrush}" />
    <Setter Property="Padding" Value="4,3,4,3" />
    <Setter Property="FontFamily" Value="{StaticResource OpenSans}" />
    <Setter Property="FontSize" Value="13" />
    <Setter Property="FontWeight" Value="Regular" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="MaxLines" Value="1" />
    <Setter Property="TextAlignment" Value="Left" />
    <Setter Property="WatermarkTemplate">
        <Setter.Value>
            <DataTemplate>
                <ContentControl Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}" 
                                Foreground="{StaticResource GrayBrush}" 
                                Focusable="False" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type xctk:WatermarkTextBox}">
                <Border Name="Border"
                        CornerRadius="2"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Background="{TemplateBinding Background}" >

                    <Grid Margin="{TemplateBinding Padding}">
                        <ScrollViewer x:Name="PART_ContentHost"
                                      Panel.ZIndex="10" 
                                      HorizontalAlignment="Stretch"
                                      VerticalAlignment="Stretch" 
                                      HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />

                        <ContentPresenter x:Name="PART_WatermarkHost"
                                          Content="{TemplateBinding Watermark}"
                                          ContentTemplate="{TemplateBinding WatermarkTemplate}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          IsHitTestVisible="False"
                                          Margin="6,0,0,0"
                                          Visibility="Collapsed" />
                    </Grid>

                    <wpf:VisualStateManager.VisualStateGroups>
                        <wpf:VisualStateGroup x:Name="CommonStates">
                            <wpf:VisualStateGroup.Transitions>
                                <wpf:VisualTransition GeneratedDuration="0" />
                            </wpf:VisualStateGroup.Transitions>
                            <wpf:VisualState x:Name="Normal" />
                            <wpf:VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
                                        <EasingColorKeyFrame KeyTime="0" Value="{StaticResource DarkBlueColor}" />
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </wpf:VisualState>
                            <wpf:VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)">
                                        <EasingColorKeyFrame KeyTime="0" Value="{StaticResource LightGrayColor}" />
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </wpf:VisualState>
                            <wpf:VisualState x:Name="ReadOnly" />
                        </wpf:VisualStateGroup>
                    </wpf:VisualStateManager.VisualStateGroups>
                </Border>

                <ControlTemplate.Triggers>
                    <Trigger Property="Text" Value="{x:Null}">
                        <Setter TargetName="PART_WatermarkHost" Property="Visibility" Value="Visible" />
                    </Trigger>
                    <Trigger Property="Text" Value="{x:Static system:String.Empty}">
                        <Setter TargetName="PART_WatermarkHost" Property="Visibility" Value="Visible" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I have defined my styles in Styles.xaml file. 我已经在Styles.xaml文件中定义了我的样式。 and I'm referencing it in each UserControl file. 我在每个UserControl文件中都引用了它。 For example: 例如:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

And I want to apply this style for WatermarkTextBox: 我想将这种样式应用于WatermarkTextBox:

    <xctk:WatermarkTextBox Margin="0,0,0,20" Text="{Binding MyField, Mode=TwoWay}">
        <xctk:WatermarkTextBox.Watermark>
            <TextBlock Text="Watermark text"
                       Style="{x:Null}" />
        </xctk:WatermarkTextBox.Watermark>
    </xctk:WatermarkTextBox>

So, as I mentioned - I see the style is applied at runtime, but in VS2013 designer it uses default style for control. 因此,正如我提到的-我看到样式是在运行时应用的,但是在VS2013设计器中,它使用默认样式进行控制。 I want to see the right style at design time. 我想在设计时看到正确的样式。 Is it possible? 可能吗?

Are the any restrictions for resource file size of anything such? 资源文件大小是否受到任何限制? My file contains ~2000lines of XAML code and there are defined about 40 styles. 我的文件包含约2000行XAML代码,并且定义了大约40种样式。

The problem was in version mismatch of NuGet packages. 问题在于NuGet软件包的版本不匹配。 Root application was using 2.4 version and module used 2.3 version. 根应用程序使用的是2.4版本,模块使用的是2.3版本。 This mismatch also caused many TargetInvocationException and NullReferenceException exceptions. 此不匹配还会导致许多TargetInvocationException和NullReferenceException异常。

I think you should give your code of resourcedictionary 我认为您应该提供资源字典的代码

to App.xaml. 到App.xaml。 And then call it in UserControl 然后在UserControl中调用它

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

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