zoukankan      html  css  js  c++  java
  • Windows Phone 8.1中AppBarToggleButton的绑定问题

    在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}" />

    这样就可以同步属性的变化了。

  • 相关阅读:
    分分钟搞定Python之排序与列表
    分分钟搞定Python之排序与列表
    联系我
    联系我
    联系我
    联系表单 1_copy
    联系表单 1_copy
    联系表单 1_copy
    联系表单 1
    联系表单 1
  • 原文地址:https://www.cnblogs.com/yanxiaodi/p/4542663.html
Copyright © 2011-2022 走看看