您的位置:首页 > 移动开发

Windows Phone 8.1中AppBarToggleButton的绑定问题

2015-05-31 21:02 387 查看
在WP8.1中,应用栏按钮已经可以支持绑定了,而且提供了一种AppBarToggleButton类型,相当于一种开关按钮,这种按钮有一个属性IsChecked,标记是否为选中状态。

于是想当然的,将IsChecked绑定到某个属性上,并设置为双向绑定。结果却发现,不起作用,该属性变化时,无法通知AppBarToggleButton的IsChecked属性进行更改。

于是写了一个扩展属性来实现这个目的,代码如下:

public class ToggleButtonProperties
{
public static readonly DependencyProperty IsCheckedProperty =
DependencyProperty.RegisterAttached("IsChecked",
typeof(bool),
typeof(ToggleButtonProperties),
new PropertyMetadata(false, OnChanged));

public static bool GetIsChecked(DependencyObject obj)
{
return (bool)obj.GetValue(IsCheckedProperty);
}
public static void SetIsChecked(DependencyObject obj, bool value)
{
obj.SetValue(IsCheckedProperty, value);
}

private static void OnChanged(DependencyObject o,
DependencyPropertyChangedEventArgs args)
{
ToggleButton tb = o as ToggleButton;
if (null != tb)
tb.IsChecked = (bool)args.NewValue;
}
}


使用的时候这样绑定:

<AppBarToggleButton Icon="Comment" Label="评论" controlHelper:ToggleButtonProperties.IsChecked="{Binding IsShowComments}" Command="{Binding CommandNavToComments}" />


这样就可以同步属性的变化了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: