【问题标题】:WPF Custom Control- Generic.xaml root element not supported by visual designerWPF 自定义控件 - 可视化设计器不支持 Generic.xaml 根元素
【发布时间】:2014-02-25 14:37:36
【问题描述】:

我正在尝试自定义扩展 WPF 工具包的 RichTextBoxFormatBar 工具,因为我想去掉一些开箱即用的功能。

我已经创建了一个自定义控件,并将原始工具源代码复制到Themes\Generic.xaml

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"
xmlns:conv="clr-namespace:Xceed.Wpf.Toolkit.Core.Converters;assembly=Xceed.Wpf.Toolkit" 
xmlns:MyNamespace="clr-namespace:IsesTextEditor">

<conv:ColorToSolidColorBrushConverter x:Key="ColorToSolidColorBrushConverter" />

<ControlTemplate TargetType="{x:Type MyNamespace:IsesFormatBar}" x:Key="IsesFormatTemplate">
    <Border Background="Transparent"
          Cursor="Hand"
          ToolTip="Click to Drag">
        <StackPanel VerticalAlignment="Center"
                 Width="75">
            <Line SnapsToDevicePixels="True"
              Stretch="Fill"
              StrokeDashArray="1,2"
              StrokeThickness="1"
              X1="0"
              X2="1"
              Margin=".5">
                <Line.Stroke>
                    <SolidColorBrush Color="Gray" />
                </Line.Stroke>
            </Line>
            <Line SnapsToDevicePixels="True"
              Stretch="Fill"
              StrokeDashArray="1,2"
              StrokeThickness="1"
              X1="0"
              X2="1"
              Margin=".5">
                <Line.Stroke>
                    <SolidColorBrush Color="Gray" />
                </Line.Stroke>
            </Line>
            <Line SnapsToDevicePixels="True"
              Stretch="Fill"
              StrokeDashArray="1,2"
              StrokeThickness="1"
              X1="0"
              X2="1"
              Margin=".5">
                <Line.Stroke>
                    <SolidColorBrush Color="Gray" />
                </Line.Stroke>
            </Line>
        </StackPanel>
    </Border>
</ControlTemplate>

design blah blah design


                </StackPanel>

            </StackPanel>
        </Grid>
    </Border>
</ControlTemplate>

<!-- =================================================================== -->
<!-- Style                                                               -->
<!-- =================================================================== -->
<Style TargetType="{x:Type MyNamespace:IsesFormatBar}">
    <Setter Property="Template"
          Value="{StaticResource richTextBoxFormatBarTemplate}" />
    <Setter Property="Effect">
        <Setter.Value>
            <DropShadowEffect BlurRadius="5"
                          Opacity=".25" />
        </Setter.Value>
    </Setter>
    <Setter Property="Background"
          Value="Transparent" />
    <Setter Property="IsTabStop"
          Value="false" />
</Style>

抱歉,很多 Xaml!

我有Ises.FormatBar.cs,它包含我所有的代码/逻辑并继承自Control.

当我尝试查看 Generic.xaml 的设计器时,我没有看到设计器和消息“有意留空。视觉设计器不支持文档根元素。

可能是我的 xaml 代码未正确绑定到 Ises.FormatBar.cs 的问题?

【问题讨论】:

    标签: c# wpf xaml generics


    【解决方案1】:

    当我尝试查看 Generic.xaml 的设计器时,我没有得到任何设计器并且 消息“故意留空”。

    这很好。这是因为您试图在设计器中查看 ResourceDictionary。

    这是唯一的问题吗? 我还猜测,当您将此自定义控件用于其他用户控件时,您不会看到预期的视图。

    理想情况下,您不应该在自定义控件样式中包含“x:Key="IsesFormatTemplate"。这样它就可以应用于所有用法,而无需明确包含样式。删除键或包含 @987654321 @ 在所有用法中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 2010-12-22
      • 1970-01-01
      相关资源
      最近更新 更多