How do I add a behavior to ApplicationBarIconButton in C#? - windows-phone-7

I am attempting to add items to an application bar with behavoirs.
In xaml they look like:
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True"
IsMenuEnabled="True">
<shell:ApplicationBarIconButton x:Name="Save"
IconUri="/resources/icons/appbar.check.rest.png"
Text="Save" />
<shell:ApplicationBarIconButton x:Name="Cancel"
IconUri="/resources/icons/appbar.cancel.rest.png"
Text="Cancel" />
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
<i:Interaction.Behaviors>
<Behaviors:ApplicationBarIconButtonCommand TextKey="Save"
CommandBinding="{Binding SaveEventSetupCommand}" />
<Behaviors:ApplicationBarIconButtonCommand TextKey="Cancel"
CommandBinding="{Binding CancelEventSetupCommand}" />
</i:Interaction.Behaviors>
For multi language support I need to add something like:
Text="{Binding Path=Localizedresources.lblCourse, Source={StaticResource LocalizedStrings}}"
to each button. It would appear that this cannot be done in xaml, hence the use of code.
The button is added in this code:
ApplicationBarIconButton appBarSaveButton = new ApplicationBarIconButton(
new Uri("/resources/icons/appbar.check.rest.png", UriKind.Relative))
{ Text = "Test" };
ApplicationBar.Buttons.Add(appBarSaveButton);
I just can't figure how to add the behavior. This is my start point:
WP7Contrib.View.Controls.Behaviors.ApplicationBarIconButtonCommand
ibc = new WP7Contrib.View.Controls.Behaviors.ApplicationBarIconButtonCommand
{ TextKey = "Test" };
Basically I am looking for a working sample if anyone can oblige.
Thanks

Based on Matt's answer this is my solution:
Add this at the top of the xaml page:
xmlns:Preview="clr-namespace:Phone7.Fx.Preview;assembly=Phone7.Fx.Preview"
and this inside the Grid at the bottom:
<Preview:BindableApplicationBar x:Name="AppBar" BarOpacity="1.0" IsVisible="{Binding IsBarVisible}" >
<Preview:BindableApplicationBarIconButton
Command="{Binding SaveCommand}"
Text="{Binding Path=Localizedresources.lblSave, Source={StaticResource LocalizedStrings}}"
IconUri="/resources/icons/appbar.check.rest.png" />
<Preview:BindableApplicationBarIconButton
Command="{Binding CancelCommand}"
Text="{Binding Path=Localizedresources.lblCancel, Source={StaticResource LocalizedStrings}}"
IconUri="/resources/icons/appbar.cancel.rest.png" />
</Preview:BindableApplicationBar>
References:
http://blog.humann.info/post/2010/08/27/How-to-have-binding-on-the-ApplicationBar.aspx
http://www.codeproject.com/KB/windows-phone-7/CommandToAppBarWP7.aspx?display=Mobile

You cannot specify the Text property of an ApplicationBarIconButton to a resource in XAML, which you've already worked out. To create a behavior and attached it in code you use code similar to the following (modified from an app I'm working on right now):
((ApplicationBarIconButton)this.ApplicationBar.Buttons[0].Text = Strings.NewContact;
var newBehavior = new ApplicationBarButtonNavigation
{
ButtonText = Strings.NewContact,
NavigateTo = new Uri("/views/ContactView.xaml", UriKind.RelativeOrAbsolute),
};
Interaction.GetBehaviors(this).Add(newBehavior);
The principal is the same for your scenario: create the behavior, and then use Interaction.GetBehaviors(this).Add(yourBehavior);
NOTE: In the above examples this refers to the code-behind for the view and is not in the view model.

You can definitely make the ApplicationBar bindable by using the wrapper from http://blog.humann.info/post/2010/08/27/How-to-have-binding-on-the-ApplicationBar.aspx
Not sure about adding commands but this shoudl be possible with the same technique.

Related

Xamarin: Binding grouped RadioButton isChecked property with a viewModel

Just to be clear I'm talking here about the RadioButton released with Xamarin 4.6 in 2006.
My aim here is to create one binding which will work with multiple RadioButtons in the same group. Ultimately so I can leave my screen and come back to it and set the last selected RadioButton item.
So I know you can set a true of false property binding to the IsChecked property. However, this single item has no context of which item it is.
I know you can also set a binding context to a page or a control... so I guess I could set a context to a StackLayout for all the items inside it... but this still won't help.
This is something like what I want to achieve... yes I won't acctually have 10, I'm just making a point that I don't want to have to use lots of different bindings..
<RadioButton GroupName="Numbers" Text="One"
IsChecked="{Binding IsCheckedNumbersgroup}" />
<RadioButton GroupName="Numbers" Text="Two"
IsChecked="{Binding IsCheckedNumbersgroup}" />
....
<RadioButton GroupName="Numbers" Text="Ten"
IsChecked="{Binding IsCheckedNumbersgroup}" />
If we binding the same source with multi RadioButton , it will cause issue as when you click one of them , other RadioButton will also been effected .
So in your case if you do not want to define multi property , you could define a List and binding them with the item .
in Xaml
Command, which is executed when the RadioButton is selected.
<RadioButton GroupName="Numbers" Text="One" IsChecked="{Binding MySource[0]}" Command="{Binding ButtonCommand}" CommandParameter="0" />
<RadioButton GroupName="Numbers" Text="Two" IsChecked="{Binding MySource[1]}" Command="{Binding ButtonCommand}" CommandParameter="1" />
<RadioButton GroupName="Numbers" Text="Three" IsChecked="{Binding MySource[2]}" Command="{Binding ButtonCommand}" CommandParameter="2" />
in ViewModel
public ObservableCollection<bool> MySource { get; set; }
public xxxViewModel()
{
MySource = new ObservableCollection<bool>() {true,false,false };
ButtonCommand = new Command((org)=> {
var index = int.Parse(org.ToString());
App.Current.MainPage.DisplayAlert("Title","the status of RadioButton"+(index+1).ToString()+"is"+MySource[index].ToString() ,"OK");
});
}

Tab bar as a filter button on windows phone

How can i create tab bar like this with xaml ? , with clicking on them , i wanna do filter on list of numbers with linq .
i am beginner on Xaml .
Thanks
You could use something like this
var qry = MyDB.TableName.Where(u => u.StartsWith == "F").ToList();
That is a horizontal ListBox which has a Tap handler to refresh the grid below.
It is hard to write a complete answer because the code depends on your current implementation.
I Created that bar by Pivot Control :
<phone:Pivot Height="40" Background="#F9A11D" SelectionChanged="Pivot_SelectionChanged" >
<phone:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock FontSize="24" Text="{Binding}" Margin="0,-5,0,0"/>
</DataTemplate>
</phone:Pivot.HeaderTemplate>
<phone:PivotItem Header="Alle"/>
<phone:PivotItem Header="A-E"/>
<phone:PivotItem Header="F-J"/>
<phone:PivotItem Header="K-O"/>
<phone:PivotItem Header="P-T"/>
<phone:PivotItem Header="U-Z"/>
</phone:Pivot>

assigning the result of variable into textblock

I'm trying to get the result of variable into a textblock.text proprety, I'm using
this code:
bool isavailable = NetworkInterface.GetIsNetworkAvailable();
result = isavailable.ToString();
<TextBlock Height="62" HorizontalAlignment="Left" Margin="12,60,0,0"
Name="textBlock1" Text="{Binding result}" VerticalAlignment="Top"
Width="400" Foreground="White" TextWrapping="Wrap" />`
Do you need to keep the binding? The simplest approach here would be to simply use:
textBlock1.Text = result;
However, the better approach would be to use a ViewModel implementing INotifyPropertyChanged with an appropriate property. Then you would make your TextBlock bind to the property, and set the property from your code. The property would raise the appropriate event, and the UI would update accordingly.

Issue binding command parameter in Silverlight

I can't seem to get this to work right. Basically, I have an ObservableCollection that is bound to a list. Inside this collection, I have an object that I need to use to pass as a variable to a Command when it's executed. My plan was to pass this as CommandParameter, but I can't get it to work. The object is actually an Enum value, but I can't get it to work with anything but static text. Below is the code, it's using a MVVM concept using Interactivity(wi)and blend's dll(sl). The property is public on the ListItem, and does implement INotifyPropertyChanged.
Thanks.
<ListBox ItemsSource="{Binding Path=MyList}" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<wi:Interaction.Triggers>
<wi:EventTrigger EventName="SelectionChanged">
<sl:InvokeDataCommand CommandParameter="{Binding MyList.ListItem.Property}" Command="{Binding Source={StaticResource Locator}, Path=MyTestPage.TestExecute}" />
</wi:EventTrigger>
</wi:Interaction.Triggers>
...
<ListBox x:Name="listBox" ItemsSource="{Binding Path=MyList}" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<wi:Interaction.Triggers>
<wi:EventTrigger EventName="SelectionChanged">
<sl:InvokeDataCommand CommandParameter="{Binding ElementName=listBox, Path=SelectedItem.Property}" Command="{Binding Source={StaticResource Locator}, Path=MyTestPage.TestExecute}" />
</wi:EventTrigger>
</wi:Interaction.Triggers>
How about doing this? You want to bind to the selectedItem, not ?
MyList is your ObservableCollection ?

Pushpin with EventToCommand

I am trying to adapt the UsingBingMaps sample included in the Windows Phone 7 TrainingKit (http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&FamilyID=ca23285f-bab8-47fa-b364-11553e076a9a) to use MVVM-Light toolkit. I am trying to set up a command to the Pushpin's MouseLeftButtonUp event using EventToCommand but the command does not get executed. Below is the code of the pushpin:
<my:Pushpin Style="{StaticResource PushpinStyle}"
Location="{Binding Location}"
Background="{Binding TypeName, Converter={StaticResource PushpinTypeBrushConverter}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<cmd:EventToCommand Command="{Binding DataContext.PushpinClickCommand, ElementName=HomePage}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Image Source="{Binding Icon}" />
</my:Pushpin>
Am I missing anything? Was anyone able to use EventToCommand with the Pushpin object?
What are you trying to do with the command?
I can get a command to fire using this, but i cant seem to get the object details from the arguments passed.
I have used this in silverlight, which I think is directly usable in WP7 (please correct me if I am mistaken)
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<cmd:EventToCommand Command="{Binding Path=pinSelCommand}" PassEventArgsToCommand="True" ></cmd:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
In my viewmodel constructor I have
PolySelCommand = new RelayCommand<MouseButtonEventArgs>(PolySelCommandExecute);
in viewmodel class
public RelayCommand<MouseButtonEventArgs> pinSelCommand{ get; set; }
private voidpinSelCommandExecute(MouseButtonEventArgs e)
{
// < Your code >
}
Hope this helps out. If you work out how to pass the object details, please post back as I have problem on this post

Resources