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

时间:2015-03-25 18:28:11

标签: c# wpf xaml

在我的项目中,我正在使用Extended WPF Toolkit。我不得不覆盖WatermarkTextBox的默认样式。乍一看没问题 - 应用程序启动和样式正在应用,但是当我看到设计器时 - 除了WatermarkTextBox之外的所有控件都应用了设计而WatermarkTextBox没有。]

我的资源词典文件:

<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">

和样式(所有静态资源在开头的同一文件中定义):

<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>

我在Styles.xaml文件中定义了我的样式。我在每个UserControl文件中引用它。例如:

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

我想将此样式应用于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>

所以,正如我所提到的 - 我看到样式在运行时应用,但在VS2013设计器中它使用默认样式进行控制。我希望在设计时看到合适的款式。有可能吗?

资源文件大小的任何限制都是这样的吗?我的文件包含〜2000行的XAML代码,并且定义了大约40种样式。

2 个答案:

答案 0 :(得分:0)

问题在于NuGet包的版本不匹配。 Root应用程序使用2.4版本,模块使用2.3版本。这种不匹配还导致了许多TargetInvocationException和NullReferenceException异常。

答案 1 :(得分:0)

我认为你应该提供resourcedictionary的代码

到App.xaml。然后在UserControl中调用它