wpf ComboBox的SelectionBoxItem相关依赖属性
以前没有注意SelectionBoxItem相关依赖属性,这几天看wpf源码 特意研究了一番
<Style x:Key="ComboBoxStyle1" TargetType="{x:Type ComboBox}">
<Setter Property="FocusVisualStyle" Value="{StaticResource ComboBoxFocusVisual}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
<Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
<Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
<Setter Property="BorderThickness" Value=""/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="Padding" Value="4,3"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="Both"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid x:Name="MainGrid" SnapsToDevicePixels="true">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width=""/>
</Grid.ColumnDefinitions>
<Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Margin="" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
<Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=MainGrid}">
<Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
<ScrollViewer x:Name="DropDownScrollViewer">
<Grid RenderOptions.ClearTypeHint="Enabled">
<Canvas HorizontalAlignment="Left" Height="" VerticalAlignment="Top" Width="">
<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>
<ToggleButton BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Grid.ColumnSpan="" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxReadonlyToggleButton}"/>
<ContentPresenter ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="false" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
<ControlTemplate.Triggers>
<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="HasItems" Value="false">
<Setter Property="Height" TargetName="DropDownBorder" Value=""/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
<Setter Property="Background" Value="#FFF4F4F4"/>
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
<Trigger Property="ScrollViewer.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.Triggers>
<Trigger Property="IsEditable" Value="true">
<Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}"/>
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Padding" Value=""/>
<Setter Property="Template" Value="{StaticResource ComboBoxEditableTemplate}"/>
</Trigger>
</Style.Triggers>
</Style>
可以看到在文本模式下 ContentPresenter 相关依赖属性 ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}"是通过这几个SelectionBoxItem属性绑定的
以下为ComboBox相关源码
通过获取InternalSelectedItem为ComboBoxItem的content,contentTemplete,contentStringFormat属性传递给internalSelectedItem,itemTemplate ,itemStringFormat
private void Update()
{
if (this.IsEditable)
{
this.UpdateEditableTextBox();
}
else
{
this.UpdateSelectionBoxItem();
}
}
private void UpdateSelectionBoxItem()
{
object internalSelectedItem = base.InternalSelectedItem;
DataTemplate itemTemplate = base.ItemTemplate;
string itemStringFormat = base.ItemStringFormat;
ContentControl control = internalSelectedItem as ContentControl;
if (control != null)
{
internalSelectedItem = control.Content;
itemTemplate = control.ContentTemplate;
itemStringFormat = control.ContentStringFormat;
}
if (this._clonedElement != null)
{
this._clonedElement.LayoutUpdated -= new EventHandler(this.CloneLayoutUpdated);
this._clonedElement = null;
}
if (((itemTemplate == null) && (base.ItemTemplateSelector == null)) && (itemStringFormat == null))
{
DependencyObject d = internalSelectedItem as DependencyObject;
if (d != null)
{
this._clonedElement = d as UIElement;
if (this._clonedElement != null)
{
VisualBrush brush = new VisualBrush(this._clonedElement) {
Stretch = Stretch.None,
ViewboxUnits = BrushMappingMode.Absolute,
Viewbox = new Rect(this._clonedElement.RenderSize),
ViewportUnits = BrushMappingMode.Absolute,
Viewport = new Rect(this._clonedElement.RenderSize)
};
DependencyObject parent = VisualTreeHelper.GetParent(this._clonedElement);
FlowDirection direction = (parent == null) ? FlowDirection.LeftToRight : ((FlowDirection) parent.GetValue(FrameworkElement.FlowDirectionProperty));
if (base.FlowDirection != direction)
{
brush.Transform = new MatrixTransform(new Matrix(-1.0, 0.0, 0.0, 1.0, this._clonedElement.RenderSize.Width, 0.0));
}
Rectangle rectangle = new Rectangle {
Fill = brush,
Width = this._clonedElement.RenderSize.Width,
Height = this._clonedElement.RenderSize.Height
};
this._clonedElement.LayoutUpdated += new EventHandler(this.CloneLayoutUpdated);
internalSelectedItem = rectangle;
itemTemplate = null;
}
else
{
internalSelectedItem = ExtractString(d);
itemTemplate = ContentPresenter.StringContentTemplate;
}
}
}
if (internalSelectedItem == null)
{
internalSelectedItem = string.Empty;
itemTemplate = ContentPresenter.StringContentTemplate;
}
this.SelectionBoxItem = internalSelectedItem;
this.SelectionBoxItemTemplate = itemTemplate;
this.SelectionBoxItemStringFormat = itemStringFormat;
} private void UpdateTextBox(string matchedText, string newText)
{
this.EditableTextBoxSite.Text = matchedText;
this.EditableTextBoxSite.SelectionStart = newText.Length;
this.EditableTextBoxSite.SelectionLength = matchedText.Length - newText.Length;
}
wpf ComboBox的SelectionBoxItem相关依赖属性的更多相关文章
- WPF系列 —— 控件添加依赖属性(转)
WPF系列 —— 控件添加依赖属性 依赖属性的概念,用途 ,如何新建与使用.本文用做一个自定义TimePicker控件来演示WPF的依赖属性的简单应用. 先上TimePicker的一个效果图. 概念 ...
- WPF入门教程系列十三——依赖属性(三)
四. 只读依赖属性 在以前在对于非WPF的功能来说,对于类的属性的封装中,经常会对那些希望暴露给外界只读操作的字段封装成只读属性,同样在WPF中也提供了只读属性的概念,如一些 WPF控件的依赖属性是只 ...
- WPF入门教程系列十一——依赖属性(一)
一.依赖属性基本介绍 本篇开始学习WPF的另一个重要内容依赖属性. 大家都知道WPF带来了很多新的特性,其中一个就是引入了一种新的属性机制——依赖属性.依赖属性出现的目的是用来实现WPF中的样式.自动 ...
- WPF学习(5)依赖属性
今天我们来学习WPF一个比较重要的概念:依赖属性.这里推荐大家看看周永恒大哥的文章,讲的确实很不错.我理解的没那么深入,只能发表一下自己的浅见.提到依赖属性,不得不说我们经常使用的传统的.net属性, ...
- WPF系列 —— 控件添加依赖属性
依赖属性的概念,用途 ,如何新建与使用.本文用做一个自定义TimePicker控件来演示WPF的依赖属性的简单应用. 先上TimePicker的一个效果图. 概念 和 用途:依赖属性是对传统.net ...
- 【WPF学习笔记】之依赖属性
概述: Windows Presentation Foundation (WPF) 提供了一组服务,这些服务可用于扩展公共语言运行时 (CLR) 属性的功能.这些服务通常统称为 WPF 属性系统.由 ...
- WPF学习笔记二之依赖属性
1.快捷生成依赖属性:propdp然后按两次tab键 2.应用场景:自定义控件 什么是依赖属性:依赖属性自己没有值,通过依赖别人(如Binding)来获得值. 依赖属性为什么会出现:控件常用字段有限, ...
- WPF入门(2)——依赖属性
今天我们说说依赖属性 什么是依赖属性? 当然,学术定义依旧Please Baidu:https://baike.baidu.com/item/%E4%BE%9D%E8%B5%96%E5%B1%9E%E ...
- WPF快速入门系列(2)——深入解析依赖属性
一.引言 感觉最近都颓废了,好久没有学习写博文了,出于负罪感,今天强烈逼迫自己开始更新WPF系列.尽管最近看到一篇WPF技术是否老矣的文章,但是还是不能阻止我系统学习WPF.今天继续分享WPF中一个最 ...
随机推荐
- poj 2689Prime Distance(区间素数)埃氏筛法
这道题的L和R都很大,所以如果直接开一个1~R的数组明显会超时.但是R-L并不大,所以我们考虑把这个区间(L--R)移动到(1--(R-L+1))这个区间再开数组(就是把每个数减L再加1).接下来先用 ...
- [POI 2000] 公共串
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2946 [算法] 建立多串后缀树 对于后缀树上的每个点 , 判断该节点所代表的等价类是 ...
- 设计四个线程,其中两个线程每次对j加1,另外两个线程每次对j减1
public class ManyThreads2 { private int j = 0; public synchronized void inc() { j++; System.out.prin ...
- 查看MySql数据库物理文件存放位置
查找数据库文件位置使用命令 show global variables like "%datadir%";
- @SpringBootApplication注解分析
首先我们分析的就是入口类Application的启动注解@SpringBootApplication,进入源码: @Target(ElementType.TYPE) @Retention(Retent ...
- VMWare虚拟机Bridged类型网卡ping不通的原因和解决办法
要使VM与局域网内的其他机器一个子网,VM的网卡设置使用桥接.本来一直正常好好的, 突然有一天,遇到VMWare虚拟机Bridged类型网卡ping不通,设置,重启,查看VM网络设置,重装VMWare ...
- linux 下 .o 文件, .a文件,.so文件的区别
最近在unbuntu环境下开发代码,由于很少使用linux开发环境,所以对linux编译方面了解更少,关于.o, .a, .so文件和可执行文件一直很困惑 今天特意查了一下关于它们的区分: .o 就相 ...
- 学习SpringMVC时遇到的一些问题
1. 找不到mapping 比较弱智的问题,忘了在对应的类上加上 @Controller 注解了 2. Model中设置的值不显示 Eclipse 自动生成的web.xml 是 2.3版本的,网上说改 ...
- Solr 6.7学习笔记(06)-- spell check
拼写检查也是搜索引擎必备的功能.Solr中提供了SpellCheckComponent 来实现此功能.我看过<Solr In Action>,是基于Solr4.X版本的,那时Suggest ...
- 洛谷P3200 [HNOI2009]有趣的数列(Catalan数)
P3200 [HNOI2009]有趣的数列 题目描述 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所有的奇数项满足 ...