In WP8.1, the app bar button already supports binding, and it provides a Appbartogglebutton type, which is the equivalent of a toggle button that has a property ischecked that the tag is selected.
So take it for granted, bind the ischecked to a property, and set it to two-way binding. The result is that it does not work, and when the property changes, the IsChecked property of the Appbartogglebutton cannot be notified of changes.
Then write an extended property to do this, the code is as follows:
Public classtogglebuttonproperties { Public Static ReadOnlyDependencyProperty Ischeckedproperty =dependencyproperty.registerattached ("IsChecked", typeof(BOOL), typeof(togglebuttonproperties),NewPropertyMetadata (false, OnChanged)); Public Static BOOLgetischecked (DependencyObject obj) {return(BOOL) obj. GetValue (Ischeckedproperty); } Public Static voidsetischecked (DependencyObject obj,BOOLvalue) {obj. SetValue (ischeckedproperty, value); } Private Static voidOnChanged (DependencyObject o, DependencyPropertyChangedEventArgs args) { ToggleButton TB= O asToggleButton; if(NULL!=TB) TB. IsChecked= (BOOL) args. NewValue; } }
This is used when binding:
<appbartogglebutton icon="Comment " label=" comments " controlhelper:togglebuttonproperties.ischecked="{Binding isshowcomments}" command= " {Binding commandnavtocomments} " />
This allows you to synchronize the changes in the properties.
Binding issues with Appbartogglebutton in Windows Phone 8.1