2012-01-25 30 views
1

我想創建一個繼承自comboBox的自定義WPF控件。到目前爲止,我創建了我的項目,並將Control從Control繼承改爲ComboBox,因爲這是我想要的。我嘗試通過進入Generic.xaml來更新視覺,並將模板從默認模板更改爲普通組合框的副本(我使用Blend創建模板的副本並將其複製)。這是它開始崩潰的地方。在WPF中創建自定義組合框

現在,我得到錯誤 找不到類型爲'Control'的靜態成員'MaxDropDownHeightProperty'。

我沒有得到任何凹線顯示錯誤的位置,但我有一個想法它在哪裏。

我想我明白這意味着模板仍然認爲它是'Control'類型的模板,但是如何將其更改爲ComboBox。

問候,

編輯:本人完全清楚,模仿COMBOX箱的模板是不是真的有用的,如果我要使用完全相同相同的,但事實是,我想evenually改變,因此需要。

這是General.xaml和我的代碼頁。這是相當長

SearchComboBox.vb:

Public Class SearchComboBox 
Inherits ComboBox 

Public Shared ReadOnly OriginalItemSourceProperty As DependencyProperty 

Private _OriginalItemSource As System.Collections.IEnumerable 

Shared Sub New() 
    'This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class. 
    'This style is defined in Themes\Generic.xaml 
    DefaultStyleKeyProperty.OverrideMetadata(GetType(SearchComboBox), New FrameworkPropertyMetadata(GetType(SearchComboBox))) 

    Dim md As New FrameworkPropertyMetadata() 
    SearchComboBox.OriginalItemSourceProperty = DependencyProperty.Register("OriginalSource", GetType(System.Collections.IEnumerable), GetType(SearchComboBox), md) 

End Sub 

Public Sub New() 

    MyBase.IsEditable = True 


End Sub 

Public Property OriginalSource As System.Collections.IEnumerable 
    Get 
     Return CType(GetValue(SearchComboBox.OriginalItemSourceProperty), System.Collections.IEnumerable) 
    End Get 
    Set(ByVal value As System.Collections.IEnumerable) 
     SetValue(SearchComboBox.OriginalItemSourceProperty, value) 
     MyBase.ItemsSource = value 
    End Set 
End Property 

End Class 

Generic.xaml:

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" 
xmlns:local="clr-namespace:SeachComboBox"> 

    <Style TargetType="{x:Type local:SearchComboBox}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate> 
       <Grid x:Name="Placement" SnapsToDevicePixels="True"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="*"/> 
         <ColumnDefinition Width="Auto"/> 
        </Grid.ColumnDefinitions> 
        <Popup x:Name="PART_Popup" AllowsTransparency="True" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom"> 
         <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=Placement}"> 
          <Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"> 
           <ScrollViewer x:Name="DropDownScrollViewer"> 
            <Grid RenderOptions.ClearTypeHint="Enabled"> 
             <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0"> 
              <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName=DropDownBorder}"/> 
             </Canvas> 
             <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
            </Grid> 
           </ScrollViewer> 
          </Border> 
         </Microsoft_Windows_Themes:SystemDropShadowChrome> 
        </Popup> 
        <Microsoft_Windows_Themes:ListBoxChrome x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}"/> 
        <TextBox x:Name="PART_EditableTextBox" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"> 
         <TextBox.Style> 
          <Style TargetType="{x:Type TextBox}"> 
           <Setter Property="OverridesDefaultStyle" Value="True"/> 
           <Setter Property="AllowDrop" Value="True"/> 
           <Setter Property="MinWidth" Value="0"/> 
           <Setter Property="MinHeight" Value="0"/> 
           <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
           <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/> 
           <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> 
           <Setter Property="Template"> 
            <Setter.Value> 
             <ControlTemplate TargetType="{x:Type TextBox}"> 
              <ScrollViewer x:Name="PART_ContentHost" Background="{TemplateBinding Background}" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/> 
              <ControlTemplate.Triggers> 
               <Trigger Property="IsEnabled" Value="False"> 
                <Setter Property="FontWeight" Value="Bold"/> 
                <Setter Property="Background" Value="#FFEDDDDD"/> 
               </Trigger> 
              </ControlTemplate.Triggers> 
             </ControlTemplate> 
            </Setter.Value> 
           </Setter> 
          </Style> 
         </TextBox.Style> 
        </TextBox> 
        <ToggleButton Grid.Column="1" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"> 
         <ToggleButton.Style> 
          <Style TargetType="{x:Type ToggleButton}"> 
           <Setter Property="OverridesDefaultStyle" Value="True"/> 
           <Setter Property="IsTabStop" Value="False"/> 
           <Setter Property="Focusable" Value="False"/> 
           <Setter Property="ClickMode" Value="Press"/> 
           <Setter Property="Template"> 
            <Setter.Value> 
             <ControlTemplate TargetType="{x:Type ToggleButton}"> 
              <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" RoundCorners="False" SnapsToDevicePixels="True" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"> 
               <Path x:Name="Arrow" Data="M0,0L3.5,4 7,0z" Fill="Black" HorizontalAlignment="Center" Margin="0,1,0,0" VerticalAlignment="Center"/> 
              </Microsoft_Windows_Themes:ButtonChrome> 
              <ControlTemplate.Triggers> 
               <Trigger Property="IsChecked" Value="True"> 
                <Setter Property="RenderPressed" TargetName="Chrome" Value="True"/> 
               </Trigger> 
               <Trigger Property="IsEnabled" Value="False"> 
                <Setter Property="Fill" TargetName="Arrow" Value="#FFEDDDDD"/> 
               </Trigger> 
              </ControlTemplate.Triggers> 
             </ControlTemplate> 
            </Setter.Value> 
           </Setter> 
          </Style> 
         </ToggleButton.Style> 
        </ToggleButton> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsKeyboardFocusWithin" Value="True"> 
         <Setter Property="Foreground" Value="Black"/> 
        </Trigger> 
        <Trigger Property="IsDropDownOpen" Value="True"> 
         <Setter Property="RenderFocused" TargetName="Border" Value="True"/> 
        </Trigger> 
        <Trigger Property="HasItems" Value="False"> 
         <Setter Property="Height" TargetName="DropDownBorder" Value="95"/> 
        </Trigger> 
        <Trigger Property="IsEnabled" Value="False"> 
         <Setter Property="FontWeight" Value="Bold"/> 
         <Setter Property="Background" Value="#FFEDDDDD"/> 
        </Trigger> 
        <Trigger Property="IsGrouping" Value="True"> 
         <Setter Property="ScrollViewer.CanContentScroll" Value="False"/> 
        </Trigger> 
        <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="True"> 
         <Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/> 
         <Setter Property="Color" TargetName="Shdw" Value="#71000000"/> 
        </Trigger> 
        <Trigger Property="CanContentScroll" SourceName="DropDownScrollViewer" Value="False"> 
         <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}"/> 
         <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
</ResourceDictionary> 

回答

1
<Style TargetType="{x:Type local:SearchComboBox}" BasedOn="{x:Type ComboBox}"> 
+0

我,我發現我的問題是什麼。這不完全是你說的,但足夠接近。我需要將TargetType放在ControlTemplate標記中 –