I am having issues with using a ContextMenu within a ListBox. One of the items of my ContextMenu is used to pass the name of the item via querystring to another page in my application. As of now I am using the SelectionChanged event of my ListBox to retrieve the name of the item selected, but this requires that the user press and then release the item to register the event. I would like to be able to get the name of the item pressed when the user presses a ListBox item down to access the ContextMenu, without pressing up.
To Note, I have tried using the KeyDown event of the ListBox for this purpose, but it did not work either. What event can I use to meet this requirement?
If you are using the binding in listbox for listbox item the you can use Tag Property of MenuItem of ContextMenu.
To Bind the tag use
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu Background="{StaticResource PhoneChromeBrush}">
<toolkit:MenuItem Foreground="{StaticResource PhoneForegroundBrush}" Header="Send" Name="Send" Tag="{Binding Name}" Click="Send_Click_1"/>
</toolkit:ContextMenuService.ContextMenu>
When you click on ContextMenuItem you can access the Tag using
var name = ((MenuItem)sender).Tag.ToString();
You can access listbox item by using this statement if you are using binding property
var listBoxItemName= ((MenuItem)sender).DataContext as ListBoxItemClass;
Here, ListBoxItemClass is the name of tha class you used for bind the property.
Try this, hope you get what you want
Related
I have an Entry and a Button. I want the command "CallWebServiceCommand" to be called when I press the button. The call to that command needs to include the url of the web service as a CommandParameter. The BindingContext is set to the ViewModel of the page.
The CommandParameter property of the button needs to reference the Text property of the entry. In WPF, I could do something like this:
<Button Text="Call web service" Command="{Binding CallWebServiceCommand}" CommandParameter="{Binding ElementName=url, Path=Text}" />
I know that it's not possible to have multiple binding contexts per view, but what would be a good workaround for this particular situation?
This is a bit of a hack, but it's worked for us in the past:
Use the ViewModel as a "relay" for the view. To do this, create a String property on your ViewModel that the text field binds its Text property to, and bind the CommandParameter of the button to this property. If you raise the PropertyChanged event for this "parameter" property, the command will supply the updated value to the method specified as the command's Action. It's certainly non-ideal, but it does work as a poor man's replacement for RelativeSource binding.
I am attempting to write an applicaiton for WF 7.5 that includes an alphabetical list of recipes. I am using a LongListSelector to display this list as a jump list. Currently, the user can tap the category heading in the list and a grid appears with all of the letters of the alphabet (just like in the music app). Unfortunately, right now the user can select a greyed-out letter from the list and the LLS will put them in the adjacent category. What I would like to happen is for the user to remain in the grid view until they select a category with items in it, or they hit back. I have a variable that tells me if the category has items in it, but im not sure how to use it to disable specific categories in the grid view. I would appreciate any help you can provide!
Just bind your variable to IsHitTestVisible property of the main element in your GroupItemTemplate. Just like that:
<LongListSelector.GroupHeaderTemplate>
<Border IsHitTestVisible="{Binding CategoryHasItems}">
<TextBlock Text="{Binding CategoryTitle}"/>
</Border>
</LongListSelector.GroupHeaderTemplate>
<TextBlock Text="{Binding Name}"/>
Binding works fine when I first navigate to a page containing the code above. Then I navigate forward to a page that that makes changes to the model (the Name property) and navigate back (with back key). The Text property, however, is still displaying the old value. How can I force the bound value to update on back navigation.
Make sure that your model implements INotifyPropertyChanged and that the PropertyChanged event is firing within the setter of Name
Using MVVM Light I have a listbox databound to a collection of several items.
One of the items is the selected one.
With this code it works perfectly and ViewModel structures are updated correctly:
<Name="listBox1"
ItemsSource="{Binding Path=Models}"
SelectedItem="{Binding Path=csProfile.Model, Mode=TwoWay}">
My problem is that when I enter the page if the selected Item is not in the first items it is not visible and the user does not know what was the previous selection.
How could I force the Listbox to always show the Selected Item?
Possibly via properties or Binding.
M
There's no property you can bind to to set what's visible. Instead call ScrollToVerticalOffset() on the ScrollViewer inside the ListBox.
I think you are looking for the ScrollIntoView method. A similar topic was discussed here:
Automatic Scrolling in a Silverlight List Box
The API reference is here: http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.scrollintoview(VS.95).aspx
Calling UpdateLayout() before calling ScrollIntoView on the selected item seems to be necessary.
I have a Listbox with Button and Textblock as ItemTemplate. Button has an onClick event.
All i want is to get the index of the clicked item, but SelectedIndex property in my ListBox is always -1!
How can I get the index of the clicked item?
The problem is that the Button control is swallowing the mouse events to provide the Click behavior, so the ListBox never receives any events to tell it that the selection has changed.
As #Alexander suggests, you could use an MVVM-style approach with commands to handle the action in a view model and pass the data context as the parameter.
Alternatively, you could replace the Button with any other layout control and either use the gesture service from the Silverlight Toolkit or use the regular MouseLeftButtonUp event. In either instance, the mouse events will bubble up and enable the ListBox to handle selection.
You might want to actually select something in your listbox then :p
If you want to get data object associated with current ListItem use RouteCommand. Here is example
<ListView.ItemTemplate>
<DataTemplate>
<Button Margin="5,0" Command="{StaticResource ButtonCommand}"
CommandParameter="{Binding}">
</DataTemplate>
<ListView.ItemTemplate>
You need also define Command at least in ListView.Resources
<RoutedCommand x:Key="ButtonCommand"/>
And then setup CommandBinding like this
<ListView.CommandBindings>
<CommandBinding Command="{StaticResource ButtonCommand}" Executed="Button_Click"/>
</ListView.CommandBindings>
In the end write handler
Button_Click(object sender, ExecutedRoutedEventArgs e)
{
e.Parameter// object that you Specified in CommandParameter
}
In advance you can use any MVVM framework, define all command and commands logic in model and then just bind this commands to corresponding elements.