2012-11-13 28 views
1

有沒有辦法像使用用戶控件一樣使用可視化XAML設計器來編寫WPF 自定義控件有沒有一種方法可視化編輯WPF自定義控件?

據我所見,在Visual Studio 2010和2012中沒有這樣的東西。我也看了一下Expression Blend 4,他們都沒有支持。

我覺得這很難相信,因爲在我看來,這將是一個顯而易見的必要特徵。

爲了清楚起見,我正在尋找一個XAML編輯器,我可以直觀地看到XAML作爲渲染控件的結果,就像我在使用它時一樣,就像在Visual Studio 2010中創作用戶控件時一樣。

+0

不創建用戶控件,但然後編輯後面的代碼從控制派生,而不是用戶控制做到這一點? http://msdn.microsoft.com/en-us/library/ms745025.aspx#models_for_control_authoring – Paparazzi

回答

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我生成項目,然後單擊用戶後控制設計師。

相關問題