/// <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)的更多相关文章

  1. WPF 精修篇 附加属性

    原文:WPF 精修篇 附加属性 微软把DLL都开源了  今天看了一下 很多WPF实现内容都在里面 https://referencesource.microsoft.com/ 说附加属性 附加属性 是 ...

  2. WPF属性(二)附加属性

    原文:WPF属性(二)附加属性 附加属性是说一个属性本来不属于某个对象,但由于某种需求而被后来附加上,也就是把对象放入一个特定环境后对象才具有的属性就称为附加属性,附加属性的作用就是将属性与数据类型解 ...

  3. WPF教程:附加属性

    一.附加属性的特点1.特殊的依赖属性2.用于非定义该属性的类 例如Grid面板的RowDefinition.ColumnDefinition.Canvas面板的Left.RightDockPanel面 ...

  4. WPF 中使用附加属性,将任意 UI 元素或控件裁剪成圆形(椭圆)

    不知从什么时候开始,头像流行使用圆形了,于是各个平台开始追逐显示圆形裁剪图像的技术.WPF 作为一个优秀的 UI 框架,当然有其内建的机制支持这种圆形裁剪. 不过,内建的机制仅支持画刷,而如果被裁剪的 ...

  5. WPF 依赖属性&附加属性

    依赖属性 暂无 附加属性 1.在没有控件源码的前提下增加控件的属性 2.多个控件需要用到同一种属性 使用附加属性可以减少代码量,不必为每一个控件都增加依赖属性 3.属性不确定是否需要使用 在某些上下文 ...

  6. WPF设置ListBoxItem失去焦点时的背景色

    <!--全局ListBoxItem--> <Style TargetType="ListBoxItem"> <Style.Resources> ...

  7. 由一次PasswordBox密码绑定引发的疑问 ---> WPF中的附加属性的定义,以及使用。

    1,前几天学习一个项目的时候,遇到了PasswordBox这个控件,由于这个控件的Password属性,不是依赖属性,所以不能和ViewModel层进行数据绑定. 2,但是要实现前后端彻底的分离,就需 ...

  8. WPF 之 利用Visibility属性进行Item模板切换

    前台Xaml如下: <Grid.Resources> <xx:AccountStatusToVisibility x:Key="AccountStatusToVisibil ...

  9. wpf 解决 WPF SelectionChanged事件向上传递造成重复执行不想执行的函数的问题

    例如 tabcontrol里有一个tabitem tabitem里有一个combox和一个datagrid tabcontrol combox datagrid都有SelectionChanged事件 ...

随机推荐

  1. 对deferred(延迟对象)的理解

    deferred对象从jQuery 1.5.0开始引入 什么是defrred对象 开发网站过程中,我们经常遇到某些耗时长的JS操作,其中,既有异步操作(如Ajax读取服务器数据),也有同步的操作(如遍 ...

  2. 【vue】饿了么项目-使用webpack打包项目

    1.vue cli给我们提供了npm run build命令打包项目,在packa.json文件中scripts对象中有build属性,当我们执行npm run build时,就执行build对应的& ...

  3. PHP扩展功能----发送邮件

    1.下载PHPMailer源码 github下载 (测试使用的是5.2.2 版本) 2.注册并登录网易邮箱(其他邮箱均可)[用于配置用户名和三方登录授权码,以及发送人邮箱地址]  (1)开启POP3协 ...

  4. 【题解】洛谷P1373 小a和uim之大逃离(坐标DP)

    次元传送门:洛谷P1373 思路 设f[i][j][t][1/0]表示走到(i,j)时 小a减去uim的差值为t 当前是小a取(0) uim取(1) 那么转移就很明显了 f[i][j][t][]=(f ...

  5. 如何处理Entity Framework / Entity Framework Core中的DbUpdateConcurrencyException异常(转载)

    1. Concurrency的作用 场景有个修改用户的页面功能,我们有一条数据User, ID是1的这个User的年龄是20, 性别是female(数据库中的原始数据)正确的该User的年龄是25, ...

  6. 依赖项从GIT上拉下来出现黄色三角形解决方法

    1.进入程序包管理器设置 2.添加程序包源=>输入名称(自己定\默认)=>NuGet源地址https://nuget.org/api/v2/(2019年1月30号可用)更新确认后重生项目即 ...

  7. JAVA中的一些内置方法

    Math 函数: Math.E //自然常数e Math.abs(12.3); //返回该值的绝对值 Math.ceil(12.3); //向上取整 Math.floor(12.3); //向下取整 ...

  8. ios开发遇到的问题

    运行后界面空白,Xcode跳转到APPDelegate.swift文件提示如下 第一种可能原因: 做输出口后在代码中重新命名了输出口 解决方法: 右键控件关闭输出口的连接,变回+号,将它重新连到代码的 ...

  9. XIB

    Xib加载的几种方法 - 简书 一天一点xib_10说说原理.优化方面的东西吧 - 简书 自定义View的封装和xib文件的使用详解|xiaoyou's blog 纯代码封装自定义View和XIB封装 ...

  10. css一边固定,另一边自适应的方法

    第一种: 第二种: