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事件 ...
随机推荐
- 【[国家集训队]Crash的数字表格 / JZPTAB】
这道题我们要求的是 \[\sum_{i=1}^N\sum_{j=1}^Mlcm(i,j)\] 总所周知\(lcm\)的性质不如\(gcd\)优雅,但是唯一分解定理告诉我们\(gcd(i,j)\time ...
- 折腾一下WebSocket的ArrayBuffer传输方式
前言 之前写WebSocket都是基于文本传输的,后来准备升级项目,于是打算尝试一下arraybuffer传输方式,由于是第一次使用javascript处理字符串转arraybuffer,不过真的 ...
- geth --rpcaddr
当想要实现从另一台电脑连接本电脑上开启的geth客户端时,需要将--rpcaddr设置为本电脑的ip地址,如下: geth --datadir data0 --networkid --port --r ...
- 如何异步的处理restful服务(基础)
1.使用Runnable 2.使用DeferredResult 3.异步处理的一些配置 正常请求方式 package com.nxz.controller; import lombok.extern. ...
- AWR报告分析
AWR报告分析 awr报告是oracle 10g下提供的一种性能收集和分析工具,它能提供一个时间段内整个系统资源使用情况的报告,通过这个报告,我们就可以了解一个系统的整个运行情况,这就像一个人全面的体 ...
- Angular动态表单生成(三)
ng-dynamic-forms实践篇(上) 定个小目标 先来定个小目标吧,我们要实现的效果: 动态生成一个表单,里面的字段如下: 字段名称 字段类型 验证 备注 姓名 text 必填,长度小于15 ...
- php版本低更换php版本-问题以解决
Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable ...
- docker 容器不能访问宿主端口原因
因为数据包到了eth0的 上的iptables 表,首先匹配PREROUTING 链,这个拒绝了来自docker0的流量,从而跳到input链,input没有放开服务端口,所以容器访问宿主端口失败;但 ...
- ABP在领域事件中异步调用方法抛异常
在领域事件中调用UserRegistrationManager.RegisterAsync抛异常 Call UserRegistrationManager.RegisterAsync() throw ...
- DataGuard快照(snapshot)数据库
在Dataguard中,可以将standby备库切换为snapshot快照数据库,在切换为snapshot数据库后,备库将置于可读写的模式.可用于模拟业务功能测试.在使用完成之后,可以将快照数据库切换 ...