在VS2010中无法在Designer中看到自定义WPF控件

时间:2011-08-08 20:10:03

标签: .net wpf visual-studio controls

我遇到了一个非常奇怪的问题,我无法在visual studio设计器中看到自定义WPF控件(好像找不到合适的样式),但Style在Property属性下显示Key的属性网格中正确显示:默认位于底部,并且还显示我的Control的图片正确设置样式,并正确设置了内容和文字。

我想我的问题很明显,但如果我的控件能够在属性网格中找到样式,为什么它在设计器中没有正确显示?

我的样式在Themes / Generic.xaml文件中声明,我在我的控件的静态构造函数中调用DefaultStyleKeyProperty.OverrideMetadata(typeof(DragDockPanel), new FrameworkPropertyMetadata(typeof(DragDockPanel)));。但是,我注意到我的被覆盖的OnApplyTemplate()函数根本没有被调用。

这是我在Generic.xaml中的风格:

<Style TargetType="local:DragDockPanel">
        <Setter Property="Background"
                Value="#ff000000" />
        <Setter Property="BorderBrush"
                Value="#ff333333" />
        <Setter Property="BorderThickness"
                Value="1" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:DragDockPanel">
                    <Grid>
                        <local:OuterGlowBorder OuterGlowOpacity="0.4"
                                               OuterGlowSize="15"
                                               CornerRadius="3,3,3,3"
                                               Background="{TemplateBinding Background}"
                                               BorderBrush="{TemplateBinding BorderBrush}"
                                               BorderThickness="{TemplateBinding BorderThickness}">
                            <local:InnerGlowBorder InnerGlowOpacity="1"
                                                   CornerRadius="3,3,3,3"
                                                   InnerGlowColor="#11ffffff"
                                                   InnerGlowSize="15,15,0,0"
                                                   Margin="0"
                                                   Padding="2"
                                                   ClipContent="True"
                                                   Background="Transparent"
                                                   BorderThickness="0">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="*" />
                                    </Grid.RowDefinitions>

                                    <ContentPresenter Grid.Row="1"
                                                      Content="{TemplateBinding Content}"
                                                      ContentTemplate="{TemplateBinding ContentTemplate}" />


                                    <Border Background="#7f000000"
                                            Margin="-2"
                                            Padding="3">
                                        <Grid>
                                            <Border x:Name="GripBarElement"
                                                    CornerRadius="3,3,0,0"
                                                    Background="#00ffffff"
                                                    VerticalAlignment="Top"
                                                    MinHeight="30"
                                                    Cursor="Hand"
                                                    Margin="0,0,32,0"
                                                    IsHitTestVisible="{TemplateBinding DraggingEnabled}">
                                                <Grid>
                                                    <Rectangle Opacity="0.5">
                                                        <Rectangle.Fill>
                                                            <LinearGradientBrush EndPoint="6.58300018310547,6.08300018310547"
                                                                                 StartPoint="2.31500005722046,1.81500005722046"
                                                                                 SpreadMethod="Repeat"
                                                                                 MappingMode="Absolute">
                                                                <GradientStop Color="#FFAFAFAF"
                                                                              Offset="0" />
                                                                <GradientStop Color="#00FFFFFF"
                                                                              Offset="1" />
                                                                <GradientStop Color="#00FFFFFF"
                                                                              Offset="0.339" />
                                                            </LinearGradientBrush>
                                                        </Rectangle.Fill>
                                                    </Rectangle>
                                                    <ContentPresenter Content="{TemplateBinding Header}"
                                                                      ContentTemplate="{TemplateBinding HeaderTemplate}" />

                                                </Grid>
                                            </Border>

                                            <ToggleButton x:Name="MaximizeToggleButton"
                                                          VerticalAlignment="Top"
                                                          HorizontalAlignment="Right"
                                                          IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, Path=IsMaximized}"
                                                          Margin="0,5,5,0"
                                                          Width="20"
                                                          Height="20"
                                                          Cursor="Hand">
                                                <ToggleButton.Template>
                                                    <ControlTemplate TargetType="ToggleButton">
                                                        <Border Background="#7F000000"
                                                                CornerRadius="2,2,2,2">
                                                            <VisualStateManager.VisualStateGroups>
                                                                <VisualStateGroup x:Name="FocusStates">
                                                                    <VisualState x:Name="Focused" />
                                                                    <VisualState x:Name="Unfocused" />
                                                                </VisualStateGroup>
                                                                <VisualStateGroup x:Name="CommonStates">
                                                                    <VisualState x:Name="Disabled" />
                                                                    <VisualState x:Name="Normal" />
                                                                    <VisualState x:Name="MouseOver">
                                                                        <Storyboard>
                                                                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                                                                           Storyboard.TargetName="rectangle"
                                                                                                           Storyboard.TargetProperty="(UIElement.Opacity)">
                                                                                <SplineDoubleKeyFrame KeyTime="00:00:00"
                                                                                                      Value="0.6" />
                                                                                <SplineDoubleKeyFrame KeyTime="00:00:00.2000000"
                                                                                                      Value="0.3" />
                                                                            </DoubleAnimationUsingKeyFrames>
                                                                        </Storyboard>
                                                                    </VisualState>
                                                                    <VisualState x:Name="Pressed">
                                                                        <Storyboard>
                                                                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                                                                           Duration="00:00:00.0010000"
                                                                                                           Storyboard.TargetName="rectangle"
                                                                                                           Storyboard.TargetProperty="(UIElement.Opacity)">
                                                                                <SplineDoubleKeyFrame KeyTime="00:00:00"
                                                                                                      Value="0.15" />
                                                                            </DoubleAnimationUsingKeyFrames>
                                                                        </Storyboard>
                                                                    </VisualState>
                                                                </VisualStateGroup>
                                                                <VisualStateGroup x:Name="CheckStates">
                                                                    <VisualState x:Name="Checked">
                                                                        <Storyboard>
                                                                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                                                                           Duration="00:00:00.0010000"
                                                                                                           Storyboard.TargetName="checkedArrow"
                                                                                                           Storyboard.TargetProperty="(UIElement.Opacity)">
                                                                                <SplineDoubleKeyFrame KeyTime="00:00:00"
                                                                                                      Value="1" />
                                                                            </DoubleAnimationUsingKeyFrames>
                                                                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
                                                                                                           Duration="00:00:00.0010000"
                                                                                                           Storyboard.TargetName="uncheckedArrow"
                                                                                                           Storyboard.TargetProperty="(UIElement.Opacity)">
                                                                                <SplineDoubleKeyFrame KeyTime="00:00:00"
                                                                                                      Value="0" />
                                                                            </DoubleAnimationUsingKeyFrames>
                                                                        </Storyboard>
                                                                    </VisualState>
                                                                    <VisualState x:Name="Unchecked">
                                                                        <Storyboard />
                                                                    </VisualState>
                                                                    <VisualState x:Name="Indeterminate" />
                                                                </VisualStateGroup>
                                                            </VisualStateManager.VisualStateGroups>
                                                            <Grid Height="Auto"
                                                                  Width="Auto">
                                                                <Border Margin="0,0,0,0"
                                                                        BorderBrush="#FFFFFFFF"
                                                                        BorderThickness="1,1,1,1"
                                                                        CornerRadius="1,1,1,1"
                                                                        Width="16"
                                                                        Height="16">
                                                                    <Grid>
                                                                        <Path HorizontalAlignment="Right"
                                                                              Margin="0,1.24500000476837,1.70700001716614,5.375"
                                                                              x:Name="uncheckedArrow"
                                                                              VerticalAlignment="Stretch"
                                                                              Width="6.752"
                                                                              Stretch="Fill"
                                                                              Stroke="#FFFFFFFF"
                                                                              Data="M0.5,1.005 L6.2509999,1.005 M6.25,6.8800006 L6.25,0.5 M6.2520003,1.0880001 L0.50000024,6.8800001" />
                                                                        <Path HorizontalAlignment="Stretch"
                                                                              Margin="1.45899999141693,5.74200010299683,5.78900003433228,0.878000020980835"
                                                                              x:Name="checkedArrow"
                                                                              VerticalAlignment="Stretch"
                                                                              Stretch="Fill"
                                                                              Stroke="#FFFFFFFF"
                                                                              Data="M0.5,1.005 L6.2509999,1.005 M6.25,6.8800006 L6.25,0.5 M6.2520003,1.0880001 L0.50000024,6.8800001"
                                                                              Opacity="0"
                                                                              RenderTransformOrigin="0.5,0.5">
                                                                            <Path.RenderTransform>
                                                                                <TransformGroup>
                                                                                    <ScaleTransform />
                                                                                    <SkewTransform />
                                                                                    <RotateTransform Angle="180" />
                                                                                    <TranslateTransform />
                                                                                </TransformGroup>
                                                                            </Path.RenderTransform>
                                                                        </Path>
                                                                    </Grid>
                                                                </Border>
                                                                <Rectangle Fill="#FFFFFFFF"
                                                                           RadiusX="2"
                                                                           RadiusY="2"
                                                                           Margin="1,1,1,1"
                                                                           Opacity="0"
                                                                           x:Name="rectangle" />
                                                            </Grid>
                                                        </Border>
                                                    </ControlTemplate>
                                                </ToggleButton.Template>
                                            </ToggleButton>
                                        </Grid>
                                    </Border>



                                </Grid>
                            </local:InnerGlowBorder>
                        </local:OuterGlowBorder>

                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>
                    <local:DropShadowTextBlock Text="{Binding}"
                                               FontFamily="Verdana"
                                               FontSize="14"
                                               VerticalAlignment="Center"
                                               Margin="5"
                                               DropShadowDistance="3"
                                               DropShadowAngle="45"
                                               Foreground="#ffffffff" />
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

此控件也使用其他自定义控件,这些控件都在Generic.xaml中声明,并设置了DefaultStyleKeyProperty。如果你想让我发布这些自定义控件的样式,我可以,但为了简洁,我把它们遗漏了。

以下是我如何调用控件:

<bl:DragDockPanelHost x:Name="dragDockPanelTest"
                                  HorizontalAlignment="Stretch"
                                  VerticalAlignment="Stretch"
                                  Visibility="Visible"
                                  Margin="0,0,0,0">
                <bl:DragDockPanel Name="Test1"
                                  Header="TEST"
                                  Content="WHERE AM I?"
                                  HorizontalAlignment="Stretch"
                                  VerticalAlignment="Stretch"
                                  Visibility="Visible">
                </bl:DragDockPanel>
            </bl:DragDockPanelHost>

任何帮助都会非常感激,我现在​​完全没有想法。谢谢!

编辑:

这里有一些可能有帮助的信息;我将DragDropPanelHost的样式添加到我的Generic.xaml中,它非常简单地更改了画布背景颜色。但是,设计器中的DragDropPanelHost背景颜色不会改变(重新编译后等)。

这是风格:

<Style TargetType="local:DragDockPanelHost">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <Canvas Background="BLUE">
                </Canvas>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>

这里看起来很混乱......出于某种原因,样式根本没有加载......

1 个答案:

答案 0 :(得分:0)

Brian Lagunas通过msdn解决了问题:

http://social.msdn.microsoft.com/Forums/pl-PL/wpf/thread/8e45f931-c6e3-4a70-91e5-39ef5c2a0b41?prof=required

“我立即看到了这个问题。你没有为DragDockPanelHost定义一个模板。例如,将其更改为:

  <Style TargetType="local:DragDockPanelHost">
    <Setter Property="ItemsPanel">
      <Setter.Value>
        <ItemsPanelTemplate>
          <Canvas Background="AliceBlue"/>
        </ItemsPanelTemplate>
      </Setter.Value>
    </Setter>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="local:DragDockPanelHost">
          <ItemsPresenter />
        </ControlTemplate>        
      </Setter.Value>
    </Setter>
  </Style>