在WPF中创建自定义组合框

时间:2012-01-25 14:09:42

标签: wpf combobox custom-controls

我想创建一个继承自comboBox的自定义WPF控件。到目前为止,我创建了我的项目并将继承从Control更改为ComboBox,因为这是我想要的。我尝试通过进入Generic.xaml来更新视觉效果,并将模板从默认模板更改为普通组合框的副本(我使用Blend创建模板的副本并将其复制)。这是它开始崩溃的地方。

现在我收到了错误     在“控件”类型上找不到静态成员'MaxDropDownHeightProperty'。

我没有得到任何凹槽线显示错误的位置,但我知道它在哪里。

我想我明白这意味着模板仍然认为它是'Control'类型的模板,但是如何将其更改为ComboBox。

问候,

编辑:我完全清楚,如果我要使用完全相同的东西,复制combox框的模板并不是真的有用,但事实是我想要改变它,因此需要。< / p>

这是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 个答案:

答案 0 :(得分:1)

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