如何通过依赖属性传递参数 [英] how to passing parameters through dependencyproperty
本文介绍了如何通过依赖属性传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
iam借助dependencyproperty创建属性.
hi all
iam creating a property with help of dependencyproperty.
public bool IsPurpule
{
get { return (bool)GetValue(IsPurpuleProperty); }
set { SetValue(IsPurpuleProperty, value); }
}
// Using a DependencyProperty as the backing store for IsPurpule. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsPurpuleProperty =
DependencyProperty.Register("IsPurpule", typeof(bool), typeof(mystackpanel), new UIPropertyMetadata(false, IsPurplePropertyChanged));
private static void IsPurplePropertyChanged(DependencyObject dependency, DependencyPropertyChangedEventArgs args)
{
mystackpanel stackpanel = dependency as mystackpanel;
if (stackpanel != null)
{
//if i want pass color from xaml how to pass the colr insted of brushes.purple
stackpanel.Background = Brushes.Purple;
}
}
//xaml code
//here insted of bool variable i want to pass color?
<local:mystackpanel IsPurpule="True" >
</local:mystackpanel>
如何让我知道?
在此先感谢
how can i do this let me know?
Thanks in advance
推荐答案
如果要实现与Background
属性类似的功能,则可以使用Brush
类型的DependencyProperty:
If you want to achieve similar functionality to theBackground
property, you could use aBrush
type DependencyProperty:
public Brush MyCustomColor
{
get { return (Brush)GetValue(MyCustomColorProperty); }
set { SetValue(MyCustomColorProperty, value); }
}
public static readonly DependencyProperty MyCustomColorProperty =
DependencyProperty.Register("MyCustomColor", typeof(Brush), typeof(mystackpanel));
代替bool使其成为typeof字符串
Instead of bool make it as typeof string
这篇关于如何通过依赖属性传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文