【问题标题】:Is there a way to visually author WPF Custom Controls?有没有办法直观地创作 WPF 自定义控件?
【发布时间】:2012-11-13 16:51:24
【问题描述】:

有没有一种方法可以使用可视化 XAML 设计器来创作 WPF 自定义控件,就像使用用户控件一样?

据我所知,Visual Studio 2010 和 2012 中没有类似的东西。我还查看了 Expression Blend 4,但似乎没有一个支持这一点。

我觉得这很难相信,因为在我看来这将是一个明显且必要的功能。

为了清楚起见,我正在寻找一个 XAML 编辑器,当我使用它时,我可以在其中直观地看到 XAML 的结果作为呈现的控件,就像在 Visual Studio 2010 中创作用户控件时一样。

【问题讨论】:

标签: .net wpf visual-studio custom-controls expression-blend


【解决方案1】:

这是我想出的一件事:

  1. 在 Visual Studio 2010 中创建一个新的 WPF 自定义控件项目。
  2. 添加一个名为 CustomControl1Dictionary.xaml 的新资源字典。
  3. 添加一个名为 CustomControl1View.xaml 的新用户控件。
  4. 像这样更改项目中的代码:

    CustomControl1Dictionary.xaml

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:WpfCustomControlLibrary1">
        <Style TargetType="{x:Type local:CustomControl1}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                        <Border Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}">
                            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="30" FontWeight="Bold">This freaking sucks!</TextBlock>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>    
    </ResourceDictionary>
    

    主题\Generic.xaml

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/WpfCustomControlLibrary1;component/CustomControl1Dictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    

    CustomControl1View.xaml

    <UserControl x:Class="WpfCustomControlLibrary1.CustomControl1View"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:WpfCustomControlLibrary1"
                 mc:Ignorable="d" 
                 d:DesignHeight="292" d:DesignWidth="786">
        <local:CustomControl1 />
    </UserControl>
    

这让我可以将用户控件作为弹出窗口打开,并且在我构建项目并单击用户控件设计器后,我可以看到我在自定义控件资源字典中对 XAML 所做的更改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 2014-02-08
    • 2011-04-25
    • 2021-07-26
    • 2019-11-28
    • 1970-01-01
    相关资源
    最近更新 更多