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 ...
- 8、Web Service-IDEA-jaxws规范下的 spring整合CXF
前提:开发和之前eclipse的开发有很大的不同! 1.服务端的实现 1.新建项目 此时创建的是web项目 2.此时创建的项目是不完整的需要开发人员手动补充完整 3.对文件夹的设置(满满的软件使用方法 ...
- ethereumjs-vm/examples/run-transactions-complete
1.设置账户: ethereumjs-vm/examples/run-transactions-complete/key-pair.json { "secretKey": &quo ...
- spring boot容器加载完后执行特定操作
有时候我们需要在spring boot容器启动并加载完后,开一些线程或者一些程序来干某些事情.这时候我们需要配置ContextRefreshedEvent事件来实现我们要做的事情 1.Applicat ...
- Beta Distribution
首先思考一个问题: 熟悉棒球运动的都知道有一个指标就是棒球击球率(batting average),就是用一个运动员击中的球数除以击球的总数,我们一般认为0.266是正常水平的击球率,正常范围在0.2 ...
- Oracle数据库常用命令(持续更新)
1. 查询当前用户所有的表 select * from user_tables; 2. 查询当前用户能访问的表 select * from all_tables; 3. 获取表字段 select * ...
- HDU 2307 贪心之活动安排问题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2037 今年暑假不AC Time Limit: 2000/1000 MS (Java/Others) ...
- stateless 无状态组件
使用:
- 【腾讯敏捷转型NO.1】敏捷是什么鬼?
“敏捷是什么鬼” 最近对外进行<腾讯产品敏捷研发体系>授课的时候,我经常可以从参课学员的眼睛里找到这句话. 通常我会鼓励大家,说:“告诉大家一个好消息,你们今天所有的疑问都是有答案的,唯一 ...
- Unity 4.7 导出工程在XCode10.1上编译报错
Unity 4.7 导出工程在XCode 10.1上编译报错,而在XCode 9.3上是可以正常编译运行的.原因是Unity4.7所依赖的头文件和库文件在XCode10上没有了,解决办法如下,把XCo ...