修改控件模板实例呢

文章来源:中山文学网  |  2021-10-29

WPF中自定义控件是可以通过修改控件模板很容易实现的,下面是一个ComboBox控件模板的实例。定义一个SolidColorBrush

SolidColorBrush x:Key=\"SolidBorderBrush\" Color=\"#888\" /

定义ComboBox控件中TextBox控件的模板

ControlTemplate x:Key=\"ComboBoxTextBox\" TargetType=\"{x:Type TextBox}\"

Border x:Name=\"PART_ContentHost\" Focusable=\"False\" Background=\"{TemplateBinding Background}\" /

/ControlTemplate

定义ComboBox控件中ToggleButton控件的模板

ControlTemplate x:Key=\"ComboBoxToggleButtonStyle\" TargetType=\"{x:Type ToggleButton}\"

Grid

lumnDefinitions

ColumnDefinition /

ColumnDefinition Width=\"19\" /

/lumnDefinitions

Border

x:Name=\"Border\"

lumnSpan=\"2\"

CornerRadius=\"0\"

BorderThickness=\"0\"

BorderBrush=\"White\"

FocusVisualStyle=\"{x:Null}\"

ckground

SolidColorBrush Color=\"#0000FF00\" /

/ckground

/Border

Border

lumn=\"0\"

CornerRadius=\"0,0,0,0\"

Margin=\"1\"

BorderThickness=\"0\" /

Image

x:Name=\"Arrow\"

lumn=\"1\"

Width =\"14\"

Height =\"13\"

Margin=\"6,6,0,0\"

HorizontalAlignment=\"Left\"

VerticalAlignment=\"Top\"

/

/Grid

/ControlTemplate

定义ComboBox的风格,应用上面定义的各个模板

德国纪念达豪集中营解放70周年 Style x:Key=\"ComboBoxStyle\" TargetType=\"{x:Type ComboBox}\"

Setter Property=\"SnapsToDevicePixels\" Value=\"true\"/

Setter Property=\"OverridesDefaultStyle\" Value=\"true\"/

Setter Property=\"rizontalScrollBarVisibility\" Value=\"Auto\"/

Setter Property=\"rticalScrollBarVisibility\" Value=\"Auto\"/

Setter Property=\"nContentScroll\" Value=\"true\"/

Setter Property=\"MinWidth\" Value=\"120\"/

Setter Property=\"MinHeight\" Value=\"20\"/

Setter Property=\"Template\"

lue

ControlTemplate TargetType=\"{x:Type ComboBox}\"

Grid

ToggleButton

Name=\"ToggleButton\"

Template=\"{StaticResource ComboBoxToggleButtonStyle}\"

lumn=\"2\"

Focusable=\"false\"

IsChecked=\"{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}\"

ClickMode=\"Press\"

/ToggleButton

ContentPresenter

Name=\"ContentSite\"

IsHitTestVisible=\"False\"

Content=\"{TemplateBinding SelectionBoxItem}\"

ContentTemplate=\"{TemplateBinding SelectionBoxItemTemplate}\"

ContentTemplateSelector=\"{TemplateBinding ItemTemplateSelector}\"

Margin=\"3,3,23,3\"

VerticalAlignment=\"Center\"

HorizontalAlignment=\"Left\" /

TextBox x:Name=\"PART_EditableTextBox\"

Style=\"{x:Null}\"

Template=\"{StaticResource ComboBoxTextBox}\"

HorizontalAlignment=\"Left\"

VerticalAlignment=\"Center\"

Margin=\"3,3,23,3\"

Focusable=\"True\"

Background=\"White\"

Visibility=\"Visible\"

IsReadOnly=\"False\"

/

!--=\"{TemplateBinding IsReadOnly}\"--

Popup

Name=\"Popup\"

Placement=\"Bottom\"

IsOpen=\"{TemplateBinding IsDropDownOpen}\"

AllowsTransparency=\"True\"

Focusable=\"False\"

PopupAnimation=\"Slide\"

Grid

Name=\"DropDown\"

SnapsToDevicePixels=\"True\"

MinWidth=\"{TemplateBinding ActualWidth}\"

MaxHeight=\"{TemplateBinding MaxDropDownHeight}\"

Border

x:Name=\"DropDownBorder\"

BorderThickness=\"1\"

BorderBrush=\"{StaticResource SolidBorderBrush}\"

ckground

SolidColorBrush Color=\"#cc336699\" /

/ckground

/Border

ScrollViewer Margin=\"4,6,4,6\" SnapsToDevicePixels=\"True\"

StackPanel IsItemsHost=\"True\" rectionalNavigation=\"Contained\" /

/ScrollViewer

/Grid

/Popup

/Grid

/ControlTemplate

/lue

/Setter

iggers

/iggers

/Style

使用上面定义的Style生成一个ComboBox控件

ComboBox Name=\"comboBoxNetwork\" Height=\"24\" Margin=\"50,50,0,0\" VerticalAlignment=\"Top\" Width=\"204\" BorderThickness=\"0\" FontFamily=\"Square721 Ex BT\" FontSize=\"14\" Foreground=\"White\" Style=\"{StaticResource ComboBoxStyle}\"/

查看本文来源

武汉男科治疗医院
南昌男科治疗哪家好
上海治疗白癜风多少钱
友情链接