`

wpf - Style setter on the attached property

    博客分类:
  • WPF
wpf 
阅读更多

I believe that you are familiar with the setter in wpf, which enables to set properties of the target control so that you can control the visual/content/template of the controls, normally what you will do is directly set the property which is owned by the target control (or properties from the parent of the target control)

 

while the setter can do more than that, it can also be used to set the attached properties,. here is some example which is used to set the control template of the Contorl called "Expander".

 

 

	<Style x:Key="NomuraExpanderStyle" TargetType="{x:Type Expander}">
		<Setter Property="FocusVisualStyle" Value="{DynamicResource NomuraFocusVisualStyle}"/>		
		<Setter Property="Background" Value="Transparent"/>
		<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
		<Setter Property="VerticalContentAlignment" Value="Stretch"/>
		<Setter Property="BorderBrush" Value="Transparent"/>
		<Setter Property="BorderThickness" Value="1"/>
		<Setter Property="Template">
			<Setter.Value>
				<ControlTemplate TargetType="{x:Type Expander}">
					<Border SnapsToDevicePixels="true">
						<DockPanel>
							<ToggleButton x:Name="HeaderSite" FocusVisualStyle="{DynamicResource NomuraFocusVisualStyle}" MinHeight="0" MinWidth="0" Style="{DynamicResource NomuraDownExpanderToggleButtonStyle}" Content="{TemplateBinding Header}" ContentTemplate="{TemplateBinding HeaderTemplate}" ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}" FontFamily="{TemplateBinding FontFamily}" FontSize="{TemplateBinding FontSize}" FontStretch="{TemplateBinding FontStretch}" FontStyle="{TemplateBinding FontStyle}" FontWeight="{TemplateBinding FontWeight}" Foreground="{DynamicResource ForegroundBrush}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" DockPanel.Dock="Top"/>
							<ContentPresenter x:Name="ExpandSite" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Focusable="false" Visibility="Collapsed" DockPanel.Dock="Bottom"/>
						</DockPanel>
					</Border>
					<ControlTemplate.Triggers>
						<Trigger Property="IsExpanded" Value="true">
							<Setter Property="Visibility" TargetName="ExpandSite" Value="Visible"/>
						</Trigger>
						<Trigger Property="ExpandDirection" Value="Right">
							<Setter Property="DockPanel.Dock" TargetName="ExpandSite" Value="Right"/>
							<Setter Property="DockPanel.Dock" TargetName="HeaderSite" Value="Left"/>
							<Setter Property="Style" TargetName="HeaderSite" Value="{StaticResource NomuraRightExpanderToggleButtonStyle}"/>
						</Trigger>
						<Trigger Property="ExpandDirection" Value="Up">
							<Setter Property="DockPanel.Dock" TargetName="ExpandSite" Value="Top"/>
							<Setter Property="DockPanel.Dock" TargetName="HeaderSite" Value="Bottom"/>
							<Setter Property="Style" TargetName="HeaderSite" Value="{StaticResource NomuraUpExpanderToggleButtonStyle}"/>
						</Trigger>
						<Trigger Property="ExpandDirection" Value="Down">
							<Setter Property="DockPanel.Dock" TargetName="ExpandSite" Value="Bottom"/>
							<Setter Property="DockPanel.Dock" TargetName="HeaderSite" Value="Top"/>
							<Setter Property="Style" TargetName="HeaderSite" Value="{StaticResource NomuraDownExpanderToggleButtonStyle}"/>
						</Trigger>
						<Trigger Property="ExpandDirection" Value="Left">
							<Setter Property="DockPanel.Dock" TargetName="ExpandSite" Value="Left"/>
							<Setter Property="DockPanel.Dock" TargetName="HeaderSite" Value="Right"/>
							<Setter Property="Style" TargetName="HeaderSite" Value="{StaticResource NomuraLeftExpanderToggleButtonStyle}"/>
						</Trigger>
					</ControlTemplate.Triggers>
				</ControlTemplate>
			</Setter.Value>
		</Setter>
		<Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}"/>
		<Setter Property="ExpandDirection" Value="Down"/>
	</Style>

 

The code is apparent on a Attached properties. 

 

<Setter Property="DockPanel.Dock" TargetName="ExpandSite" Value="Right"/>

 

What about if the attached properties is home-made one, can you do the same as well?

well the answer is Yes, you can, but you have to watch for namespace prefix. . 

 

Now, suppose that I have endow the control of the ability to swtich between themes, and I have created an attache properties to help attached the very important information to the Control which it decorate, the control is called c1:FlexGrid....

 

Now here is my Behavior's code. 

 

public class ThemeKindChangedEventArgs : EventArgs
    {
        public ThemeKind ThemeKind { get; internal set; }
    }

    public delegate void ThemeKindChanged(DependencyObject d, ThemeKindChangedEventArgs e);

    public class ThemeKindBehavior
    {
        public static readonly DependencyProperty ThemeKindProperty =
            DependencyProperty.RegisterAttached("ThemeKind", typeof (ThemeKind), typeof (ThemeKindBehavior), new PropertyMetadata(default(ThemeKind), new PropertyChangedCallback(OnThemeKindChanged)));

        public static void SetThemeKind(UIElement element, ThemeKind value)
        {
            element.SetValue(ThemeKindProperty, value);
        }

        public static ThemeKind GetThemeKind(UIElement element)
        {
            return (ThemeKind)element.GetValue(ThemeKindProperty);
        }

        public static event ThemeKindChanged ThemeKindChanged;

        public static void OnThemeKindChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (null != d)
            {
                var flexGrid = d as C1FlexGrid;
                if (flexGrid != null)
                {

                    var oldTheme = (ThemeKind) e.OldValue;
                    var theme = (ThemeKind) e.NewValue;
                    var fac = flexGrid.CellFactory as FilterRowCellFactoryWrapper;
                    fac = new FilterRowCellFactoryWrapper(theme);
                    flexGrid.CellFactory = fac;
                    if (oldTheme != theme)
                    {
                        ThemeKindChanged themeChanged = ThemeKindChanged;
                        if (themeChanged != null)
                        {
                            themeChanged(d, new ThemeKindChangedEventArgs() { ThemeKind = theme });
                        }
                    }
                }
            }

        }
    }

 

To use that, I can use the following. 

<dataitemtheme:ThemeKind x:Key="ThemeKind">Unity</dataitemtheme:ThemeKind>

    <Style  TargetType="c1:C1FlexGrid"
            x:Key="RPMC1FlexGrid"
            >
        <Setter Property="ColumnHeaderBackground" Value="{StaticResource C1FlexGridColumnHeaderBackground}" />
        <Setter Property="ColumnHeaderSelectedBackground" Value="{StaticResource C1FlexGridColumnHeaderSelectedBackground}" />
        <Setter Property="GridLinesBrush">
            <Setter.Value>
                <SolidColorBrush Color="#FFC6D7ED" />
            </Setter.Value>
        </Setter>
        <Setter Property="GroupRowBackground">
            <Setter.Value>
                <SolidColorBrush Color="#FFC6D7ED" />
            </Setter.Value>
        </Setter>
		
		
		<!-- here is where you will do the changes -->
		
		<Setter Property="local:ThemeKindBehavior.ThemeKind" Value="{DynamicResource ThemeKind}" />
    </Style>


 So the key here is the use of namespace prefixed names in the "Property" field of Setter object. 

 

 

 

分享到:
评论

相关推荐

    bootstrap-wpf-style-master 样式

    Bootstrap-WPF 样式是一种将流行的前端框架 Bootstrap 的设计风格应用于 WPF(Windows Presentation Foundation)应用程序的方法。Bootstrap 是一个广泛使用的开源工具包,主要用于构建响应式、移动设备优先的网页...

    通用WPF主题控件包rubyer-wpf-master

    通用WPF主题控件包rubyer-wpf-master是一款专为Windows Presentation Foundation (WPF) 应用程序设计的开源UI框架。它提供了丰富的主题和控件,旨在帮助开发者快速构建美观且用户友好的应用程序界面。在2.0.0版本中...

    gong-wpf-dragdrop, GongSolutions.WPF.DragDrop 库是WPF的拖动'n'拖放框架.zip

    gong-wpf-dragdrop, GongSolutions.WPF.DragDrop 库是WPF的拖动'n'拖放框架 简介GongSolutions.WPF.DragDrop 库是一个易于使用的拖拉'n'拖放框架。特性使用 MVVM: 拖放逻辑可以放在ViewModel中。 代码不需要放在in中...

    wpf-mvvm-DeskTop-Sample-master_C#_WPF_wpf客户端zfs_

    标题中的“wpf-mvvm-DeskTop-Sample-master”表明这是一个关于WPF(Windows Presentation Foundation)桌面应用程序的示例项目,使用了MVVM(Model-View-ViewModel)设计模式。这个项目是用C#编程语言编写的,面向的...

    bootstrap-wpf-style.zip

    标题"bootstrap-wpf-style.zip"暗示了这个压缩包包含了一个项目或资源集合,使得开发者能够在WPF应用中应用Bootstrap的样式。这可能是通过移植Bootstrap的CSS规则到WPF的样式系统,或者创建一个与Bootstrap类似的...

    WPF-Blockly-master.zip

    **WPF-Blockly** 是一个基于Windows Presentation Foundation (WPF) 的图形化编程工具,它为用户提供了构建和设计程序的直观界面。WPF作为Microsoft .NET Framework的一部分,主要用于构建桌面应用程序,它提供了...

    WPF - TabControl - Style用法

    学习在WPF当中如何使用Style定制TabControl的样式,包含TabControl的页面选项切换的简单动画效果。原始代码网上找的,我改了一些代码。

    C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 4

    C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手

    WPF-Samples-master_WPF基本sample_

    WPF的基本空间历程,使用.net core3.0.1版本

    基于WPF的图形化编程控件和环境WPF-Blockly-master

    【标题】"基于WPF的图形化编程控件和环境WPF-Blockly-master" 提供了一个创新的编程体验,它将传统的代码编写转变为图形化的流程图形式,使得编程变得更加直观和易于理解。WPF(Windows Presentation Foundation)是...

    AI-wpf-controls一个Wpf控件库

    在本文中,我们将深入探讨"AI-wpf-controls",这是一个专为Windows Presentation Foundation(WPF)框架设计的控件库。这个独特的库整合了多个知名控件库的优点,包括MahApps.Metro、Material-Design、HandyControl...

    WPF-Diagram-Designer:WPF图表设计器源代码

    通过深入研究WPF-Diagram-Designer的源代码(如WPF-Diagram-Designer-master文件夹中的内容),开发者不仅可以学习到如何在WPF中构建复杂的图形界面,还可以了解到图形编辑器的设计原理和实现细节,对于提升图形应用...

    WPF-MaterialDesign-master.zip_WPF_WPF非常好的界面_包括多种漂亮的皮肤_漂亮的控件_配色

    在本项目"WPF-MaterialDesign-master.zip"中,重点在于利用**Material Design**这一设计语言来增强WPF应用的视觉效果。Material Design是Google推出的一种设计规范,其灵感来源于现实世界中的纸张和墨水,强调层次感...

    wpf---StatusBar

    “wpf---StatusBar”这个标题表明我们将探讨的是WPF(Windows Presentation Foundation)框架中的StatusBar组件。WPF是.NET Framework的一部分,用于构建桌面应用程序,它提供了丰富的用户界面(UI)功能。StatusBar...

    WPF-强大的图表.zip

    **WPF - 强大的图表技术** Windows Presentation Foundation (WPF) 是Microsoft开发的一个用于构建桌面应用程序的框架,它提供了丰富的图形系统,包括对2D和3D图形的强大支持。在WPF中,开发人员可以利用各种图表...

    wpf-datagrid-access DB

    在这个“wpf-datagrid-access DB”主题中,我们将深入探讨如何利用WPF Datagrid与Microsoft Access数据库进行交互,实现数据的读取、更新和保存。 1. **WPF Datagrid简介** - Datagrid是WPF中的一个数据展示控件,...

    C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手1

    C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手

    WPF-ScreenData.zip

    超酷WPF数据可视化,全套源程序,非常适合做数据可视化的程序员使用,WPF,XAML。 找了好久的资源,附有很多很详细的插图,是大数据时代不可缺少的可视化学习数据,仅供大家学习使用请勿用做商业用途。与大家一起...

    Prism-Samples-Wpf-master11-15.zip

    Prism-Samples-Wpf-master11-15的VS2017版本实现,下载手动重新安装一下nuget包即可,方便大家学习

    practical-wpf-charts-graphics-master.rar

    该资源"practical-wpf-charts-graphics-master.rar"包含了这本书的源代码,为读者提供了丰富的实践案例和深入理解WPF图表及图形编程的宝贵材料。 WPF(Windows Presentation Foundation)是.NET Framework的一部分...

Global site tag (gtag.js) - Google Analytics