WPF RegisterAttached ListBoxItem(附加属性传递到Item)
/// <summary>
/// Controls的附加属性
/// </summary>
public class ControlsAttached : DependencyObject
{
public static CornerRadius GetCornerRadius(DependencyObject obj)
{
return (CornerRadius)obj.GetValue(CornerRadiusProperty);
} public static void SetCornerRadius(DependencyObject obj, CornerRadius value)
{
obj.SetValue(CornerRadiusProperty, value);
} // Using a DependencyProperty as the backing store for CornerRadius. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CornerRadiusProperty =
DependencyProperty.RegisterAttached("CornerRadius", typeof(CornerRadius), typeof(ControlsAttached), new PropertyMetadata(new CornerRadius())); public static Thickness GetBorderThickness(DependencyObject obj)
{
return (Thickness)obj.GetValue(BorderThicknessProperty);
} public static void SetBorderThickness(DependencyObject obj, Thickness value)
{
obj.SetValue(BorderThicknessProperty, value);
} // Using a DependencyProperty as the backing store for BorderThickness. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BorderThicknessProperty =
DependencyProperty.RegisterAttached("BorderThickness", typeof(Thickness), typeof(ControlsAttached), new PropertyMetadata(new Thickness())); public static int GetControlTag(DependencyObject obj)
{
return (int)obj.GetValue(ControlTagProperty);
} public static void SetControlTag(DependencyObject obj, int value)
{
obj.SetValue(ControlTagProperty, value);
} // Using a DependencyProperty as the backing store for ControlTag. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ControlTagProperty =
DependencyProperty.RegisterAttached("ControlTag", typeof(int), typeof(ControlsAttached), new PropertyMetadata()); public static Visibility GetIsShowControl(DependencyObject obj)
{
return (Visibility)obj.GetValue(IsShowControlProperty);
}
public static void SetIsShowControl(DependencyObject obj, Visibility value)
{
obj.SetValue(IsShowControlProperty, value);
} /// <summary>
/// 是否显示控件
/// </summary>
/// <param name="obj"></param>
/// <param name="value"></param>
// Using a DependencyProperty as the backing store for IsShowControl. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsShowControlProperty =
DependencyProperty.RegisterAttached("IsShowControl", typeof(Visibility), typeof(ControlsAttached), new PropertyMetadata(Visibility.Visible)); public static double GetAttachHeight(DependencyObject obj)
{
return (double)obj.GetValue(AttachHeightProperty);
} public static void SetAttachHeight(DependencyObject obj, double value)
{
obj.SetValue(AttachHeightProperty, value);
} // Using a DependencyProperty as the backing store for AttachHeight. This enables animation, styling, binding, etc...
public static readonly DependencyProperty AttachHeightProperty =
DependencyProperty.RegisterAttached("AttachHeight", typeof(double), typeof(ControlsAttached), new PropertyMetadata(double.NaN)); public static Brush GetMouseOverBackground(DependencyObject obj)
{
return (Brush)obj.GetValue(MouseOverBackgroundProperty);
} public static void SetMouseOverBackground(DependencyObject obj, Brush value)
{
obj.SetValue(MouseOverBackgroundProperty, value);
} // Using a DependencyProperty as the backing store for MouseOverBackground. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MouseOverBackgroundProperty =
DependencyProperty.RegisterAttached("MouseOverBackground", typeof(Brush), typeof(ControlsAttached), new PropertyMetadata(Brushes.Transparent)); public static Brush GetMouseOverBorderBrush(DependencyObject obj)
{
return (Brush)obj.GetValue(MouseOverBorderBrushProperty);
} public static void SetMouseOverBorderBrush(DependencyObject obj, Brush value)
{
obj.SetValue(MouseOverBorderBrushProperty, value);
} // Using a DependencyProperty as the backing store for MouseOverBorderBrush. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MouseOverBorderBrushProperty =
DependencyProperty.RegisterAttached("MouseOverBorderBrush", typeof(Brush), typeof(ControlsAttached), new PropertyMetadata(Brushes.Transparent)); public static Brush GetPressBackground(DependencyObject obj)
{
return (Brush)obj.GetValue(PressBackgroundProperty);
} public static void SetPressBackground(DependencyObject obj, Brush value)
{
obj.SetValue(PressBackgroundProperty, value);
} // Using a DependencyProperty as the backing store for PressBackground. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PressBackgroundProperty =
DependencyProperty.RegisterAttached("PressBackground", typeof(Brush), typeof(ControlsAttached), new PropertyMetadata(Brushes.Transparent)); public static Brush GetPressBorderBrush(DependencyObject obj)
{
return (Brush)obj.GetValue(PressBorderBrushProperty);
} public static void SetPressBorderBrush(DependencyObject obj, Brush value)
{
obj.SetValue(PressBorderBrushProperty, value);
} // Using a DependencyProperty as the backing store for PressBorderBrush. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PressBorderBrushProperty =
DependencyProperty.RegisterAttached("PressBorderBrush", typeof(Brush), typeof(ControlsAttached), new PropertyMetadata(Brushes.Transparent)); public static int GetControlEnable(DependencyObject obj)
{
return (int)obj.GetValue(ControlEnableProperty);
} public static void SetControlEnable(DependencyObject obj, int value)
{
obj.SetValue(ControlEnableProperty, value);
} // Using a DependencyProperty as the backing store for ControlEnable. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ControlEnableProperty =
DependencyProperty.RegisterAttached("ControlEnable", typeof(int), typeof(ControlsAttached), new PropertyMetadata()); public static VerticalAlignment GetVerticalAlignment(DependencyObject obj)
{
return (VerticalAlignment)obj.GetValue(VerticalAlignmentProperty);
} public static void SetVerticalAlignment(DependencyObject obj, VerticalAlignment value)
{
obj.SetValue(VerticalAlignmentProperty, value);
} // Using a DependencyProperty as the backing store for VerticalAlignment. This enables animation, styling, binding, etc...
public static readonly DependencyProperty VerticalAlignmentProperty =
DependencyProperty.RegisterAttached("VerticalAlignment", typeof(VerticalAlignment), typeof(ControlsAttached), new PropertyMetadata(VerticalAlignment.Stretch)); public static HorizontalAlignment GetHorizontalAlignment(DependencyObject obj)
{
return (HorizontalAlignment)obj.GetValue(HorizontalAlignmentProperty);
} public static void SetHorizontalAlignment(DependencyObject obj, HorizontalAlignment value)
{
obj.SetValue(HorizontalAlignmentProperty, value);
} // Using a DependencyProperty as the backing store for HorizontalAlignment. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HorizontalAlignmentProperty =
DependencyProperty.RegisterAttached("HorizontalAlignment", typeof(HorizontalAlignment), typeof(ControlsAttached), new PropertyMetadata(HorizontalAlignment.Stretch)); }
<Style x:Key="ListBoxItemStyle"
TargetType="{x:Type ListBoxItem}">
<Setter Property="HorizontalContentAlignment"
Value="Stretch"></Setter>
<Setter Property="Foreground"
Value="White"></Setter>
<Setter Property="SnapsToDevicePixels"
Value="True"></Setter>
<Setter Property="FocusVisualStyle"
Value="{x:Null}"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid>
<Border Name="Border"
BorderBrush="Transparent"
CornerRadius=""
Margin="1 1"
Height="">
<Border.Background>
<SolidColorBrush Color="Transparent"
x:Name="BorderBg"></SolidColorBrush>
</Border.Background>
<ContentPresenter VerticalAlignment="{Binding Path=(local:ControlsAttached.VerticalAlignment),RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBox}}}"
Margin="3 0" />
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Background"
TargetName="Border"
Value="#9ab6bd" />
<Setter Property="Foreground"
Value="Black"></Setter>
</Trigger>
<Trigger Property="IsMouseOver"
Value="True">
<Setter TargetName="Border"
Property="BorderBrush"
Value="#acacac"></Setter>
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="BorderBg"
Storyboard.TargetProperty="Color"
To="#4f7f8d"
Duration="0:0:0.3"></ColorAnimation> </Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="BorderBg"
Storyboard.TargetProperty="Color"
Duration="0:0:0.3">
</ColorAnimation>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger> </ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ListBox ItemsSource="{Binding datas}"
SelectedItem="{Binding data}"
DisplayMemberPath="Name"
Foreground="White"
Style="{DynamicResource MyListBoxStyle}"
local:ControlsAttached.VerticalAlignment="Bottom"
ItemContainerStyle="{DynamicResource ListBoxItemStyle}">
</ListBox>
(local:ControlsAttached.VerticalAlignment)
一定要加()否则绑定不生效!!!
实现这个是为了再ListBox设置子项对齐的方式,不会因为子项对齐方式不一样而新建Style
WPF RegisterAttached ListBoxItem(附加属性传递到Item)的更多相关文章
- WPF 精修篇 附加属性
原文:WPF 精修篇 附加属性 微软把DLL都开源了 今天看了一下 很多WPF实现内容都在里面 https://referencesource.microsoft.com/ 说附加属性 附加属性 是 ...
- WPF属性(二)附加属性
原文:WPF属性(二)附加属性 附加属性是说一个属性本来不属于某个对象,但由于某种需求而被后来附加上,也就是把对象放入一个特定环境后对象才具有的属性就称为附加属性,附加属性的作用就是将属性与数据类型解 ...
- WPF教程:附加属性
一.附加属性的特点1.特殊的依赖属性2.用于非定义该属性的类 例如Grid面板的RowDefinition.ColumnDefinition.Canvas面板的Left.RightDockPanel面 ...
- WPF 中使用附加属性,将任意 UI 元素或控件裁剪成圆形(椭圆)
不知从什么时候开始,头像流行使用圆形了,于是各个平台开始追逐显示圆形裁剪图像的技术.WPF 作为一个优秀的 UI 框架,当然有其内建的机制支持这种圆形裁剪. 不过,内建的机制仅支持画刷,而如果被裁剪的 ...
- WPF 依赖属性&附加属性
依赖属性 暂无 附加属性 1.在没有控件源码的前提下增加控件的属性 2.多个控件需要用到同一种属性 使用附加属性可以减少代码量,不必为每一个控件都增加依赖属性 3.属性不确定是否需要使用 在某些上下文 ...
- WPF设置ListBoxItem失去焦点时的背景色
<!--全局ListBoxItem--> <Style TargetType="ListBoxItem"> <Style.Resources> ...
- 由一次PasswordBox密码绑定引发的疑问 ---> WPF中的附加属性的定义,以及使用。
1,前几天学习一个项目的时候,遇到了PasswordBox这个控件,由于这个控件的Password属性,不是依赖属性,所以不能和ViewModel层进行数据绑定. 2,但是要实现前后端彻底的分离,就需 ...
- WPF 之 利用Visibility属性进行Item模板切换
前台Xaml如下: <Grid.Resources> <xx:AccountStatusToVisibility x:Key="AccountStatusToVisibil ...
- wpf 解决 WPF SelectionChanged事件向上传递造成重复执行不想执行的函数的问题
例如 tabcontrol里有一个tabitem tabitem里有一个combox和一个datagrid tabcontrol combox datagrid都有SelectionChanged事件 ...
随机推荐
- 随手练——HDU-2037 、P-2920 时间安排(贪心)
普通时间安排 HDU-2037 :http://acm.hdu.edu.cn/showproblem.php?pid=2037 选取结束时间早的策略. #include <iostream> ...
- Bootstrap--模仿官网写一个页面
本文参考Bootstrap官方文档写了简单页面来熟悉Bootstrap的栅格系统.常用CSS样.Javascript插件和部分组件. 以下html代码可以直接复制本地运行: BootstrapPage ...
- 该网页已屏蔽以下插件Adobe Flash Player
2017.6.9更新:多谢网友留言,该网页已屏蔽以下插件Adobe Flash Player解决方法:chrome://flags/#run-all-flash-in-allow-mode选择启用就O ...
- Jpa条件查询组合查询and 和 or同时用
条件查询,各个条件之间是and并且&&关系,其中地理信息省市区县,例如河北省,要包括其下属所有城市,每个城市包括下属区县,只选择河北省时候,要查询的是河北省所有的,他们之间是or 或者 ...
- JAVA中的一些内置方法
Math 函数: Math.E //自然常数e Math.abs(12.3); //返回该值的绝对值 Math.ceil(12.3); //向上取整 Math.floor(12.3); //向下取整 ...
- oracle中如何将表缓存到内存中
oracle快速将表缓存到内存中,使得访问速度加快. 共有2种方法: 1)alter table fisher cache; 2)alter table fisher storage(buffer ...
- office365离线安装
office版本是在线安装,每次安装比较麻烦,所以还是离线安装合适,这里推荐一篇博文https://www.cnblogs.com/Devopser/p/7919245.html 但是由于部署工具变化 ...
- css中的定位问题
由于我最近在修改自己的网页布局,突然发现了自己对css中的定位概念还是混淆的,于是通过查官方文档,大神博客,自己实践,重新梳理了css定位的知识点.如果有不对的地方,请指正
- (1-1)入门—最简单的树(使用json数据)
1.<!DOCTYPE html>是必须的. 2.zTree 的容器 className 别忘了设置为 "ztree". 使用ztree创建树,首先要引用ztree相关 ...
- 延迟加载图片控件--echo.js
echo.js的github地址:https://github.com/toddmotto/echo echo是一个独立的JavaScript.轻量级的.延迟图片加载插件,echo压缩后体积不到1 ...