在值更改时获取 DependencyProperty 所有者类型实例
Get DependencyProperty Owner Type instance on value change
我在我的 ActionBar 自定义控件中创建了一个依赖项 属性:
public NavigationStyle NavigationStyle
{
get { return (NavigationStyle)GetValue(NavigationStyleProperty); }
set { SetValue(NavigationStyleProperty, value); }
}
// Using a DependencyProperty as the backing store for NavigationStyle. This enables animation, styling, binding, etc...
public static readonly DependencyProperty NavigationStyleProperty =
DependencyProperty.Register("NavigationStyle", typeof(NavigationStyle), typeof(ActionBar), new
NavigationStylePropertyMetadata(NavigationStyle.TwoColumnsNavigation));
public enum NavigationStyle
{
SingleNavigation,
TwoColumnsNavigation
}
我有一个回调,当 属性 的值发生变化时,我必须编辑 ActionBar 样式(宽度):
private class NavigationStylePropertyMetadata : FrameworkPropertyMetadata
{
public NavigationStylePropertyMetadata(object defaultValue)
:base(defaultValue)
{
base.PropertyChangedCallback = (dependicyProperty, e) => {
// How can i get the instance of the ActionBar control ?
switch ((NavigationStyle)e.NewValue)
{
case NavigationStyle.SingleNavigation:
// here i need to edit the width of the ActionBar to 500px
break;
case NavigationStyle.TwoColumnsNavigation:
// and here i have to edit the ActionBar width to 700
break;
default:
break;
}
};
}
}
但我的问题是,如何在 PropertyChangeCallback
中获取 ActionBar Control(inherits from flyout) 的实例来编辑其样式?
首先,更改您的依赖项 属性 声明。
public static readonly DependencyProperty NavigationStyleProperty =
DependencyProperty.Register("NavigationStyle", typeof(NavigationStyle), typeof(ActionBar), new NavigationStylePropertyMetadata(NavigationStyle.TwoColumnsNavigation, ValueChanged));
然后将方法 ValueChanged
添加到您的 ActionBar
自定义控件 class。
private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ActionBar actionBar = d as ActionBar;
// Do other stuff
switch ((NavigationStyle)e.NewValue)
{
case NavigationStyle.SingleNavigation:
// here i need to edit the width of the ActionBar to 500px
break;
case NavigationStyle.TwoColumnsNavigation:
// and here i have to edit the ActionBar width to 700
break;
default:
break;
}
}
最后修改您的 NavigationStylePropertyMetadata
构造函数以接受 PropertyChangedCallback
。
public NavigationStylePropertyMetadata(object defaultValue, PropertyChangedCallback propertyChangedCallback)
:base(defaultValue, propertyChangedCallback)
{
// do some stuff here, or just remove this class if you dont need it and just use FrameworkPropertyMetadata
}
实际上,属性 元数据不需要自定义 class。只需像这样声明你的依赖项 属性:
public static readonly DependencyProperty NavigationStyleProperty =
DependencyProperty.Register(
"NavigationStyle",
typeof(NavigationStyle),
typeof(ActionBar),
new FrameworkPropertyMetadata(
NavigationStyle.TwoColumnsNavigation, ValueChanged));
我在我的 ActionBar 自定义控件中创建了一个依赖项 属性:
public NavigationStyle NavigationStyle
{
get { return (NavigationStyle)GetValue(NavigationStyleProperty); }
set { SetValue(NavigationStyleProperty, value); }
}
// Using a DependencyProperty as the backing store for NavigationStyle. This enables animation, styling, binding, etc...
public static readonly DependencyProperty NavigationStyleProperty =
DependencyProperty.Register("NavigationStyle", typeof(NavigationStyle), typeof(ActionBar), new
NavigationStylePropertyMetadata(NavigationStyle.TwoColumnsNavigation));
public enum NavigationStyle
{
SingleNavigation,
TwoColumnsNavigation
}
我有一个回调,当 属性 的值发生变化时,我必须编辑 ActionBar 样式(宽度):
private class NavigationStylePropertyMetadata : FrameworkPropertyMetadata
{
public NavigationStylePropertyMetadata(object defaultValue)
:base(defaultValue)
{
base.PropertyChangedCallback = (dependicyProperty, e) => {
// How can i get the instance of the ActionBar control ?
switch ((NavigationStyle)e.NewValue)
{
case NavigationStyle.SingleNavigation:
// here i need to edit the width of the ActionBar to 500px
break;
case NavigationStyle.TwoColumnsNavigation:
// and here i have to edit the ActionBar width to 700
break;
default:
break;
}
};
}
}
但我的问题是,如何在 PropertyChangeCallback
中获取 ActionBar Control(inherits from flyout) 的实例来编辑其样式?
首先,更改您的依赖项 属性 声明。
public static readonly DependencyProperty NavigationStyleProperty =
DependencyProperty.Register("NavigationStyle", typeof(NavigationStyle), typeof(ActionBar), new NavigationStylePropertyMetadata(NavigationStyle.TwoColumnsNavigation, ValueChanged));
然后将方法 ValueChanged
添加到您的 ActionBar
自定义控件 class。
private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ActionBar actionBar = d as ActionBar;
// Do other stuff
switch ((NavigationStyle)e.NewValue)
{
case NavigationStyle.SingleNavigation:
// here i need to edit the width of the ActionBar to 500px
break;
case NavigationStyle.TwoColumnsNavigation:
// and here i have to edit the ActionBar width to 700
break;
default:
break;
}
}
最后修改您的 NavigationStylePropertyMetadata
构造函数以接受 PropertyChangedCallback
。
public NavigationStylePropertyMetadata(object defaultValue, PropertyChangedCallback propertyChangedCallback)
:base(defaultValue, propertyChangedCallback)
{
// do some stuff here, or just remove this class if you dont need it and just use FrameworkPropertyMetadata
}
实际上,属性 元数据不需要自定义 class。只需像这样声明你的依赖项 属性:
public static readonly DependencyProperty NavigationStyleProperty =
DependencyProperty.Register(
"NavigationStyle",
typeof(NavigationStyle),
typeof(ActionBar),
new FrameworkPropertyMetadata(
NavigationStyle.TwoColumnsNavigation, ValueChanged));