Problem binding ICommand to event using ElementName within Pivot control - silverlight

I am trying to hook up an ICommand on the model to a button within the ItemTemplate of a Pivot control.
To get a link to the parent model from within the ItemTemplate I usually use ElementName specifying the Name I provided for the xaml page.
This works when I use a ListBox to contain the items but not a pivot control.
Does any one have any ideas or come across this problem before?
Just noticed that if I define the PivotItems in xaml the Binding works. So it is only failing when I am dynamically populating the Pivot control.
UPDATE : OK So I'm beat with this now. I have linked the event to the models ICommand in the views code behind (nasty) and I'm going to look # this later. I will post my solution here once I have found it but any help would be great.

This is a know problem in Silverlight 3. Since, WP7 uses it right now, you will face the same thing with it as well.
To fix that, wrap your DataTemplate content that you put in your ItemTemplate into a UserControl.
Look into this question for further details.
WP7: Why does a ListBox.ItemsPanel break my ElementName data binding?

Related

Dynamically Load UserControl using MVVM Light Toolkit

I searched this site and i found 2 Links
how to load wpf usercontrol in MVVM pattern
MVVM-Light: Load UserControl into Window
but still i cant find the answer to my problem and this link
MVVM-Light Locator Pattern and Reusable UserControl
i Didn't Understood.... so here is i am stating my problem which might help others struggling like me ......
I have a MainWindow which has 2 parts one has a TreeView(a
UserControl) and the other Displays different user controls(named DisplayPanel).... just like windows Explorer.
The Display Panel on the Right side will display different user controls on Clicking nodes of tree view.
and my TreeView is Itself a user Control.
How can i make this composite UI Work using MVVM. Also I am planning to use MVVM light Toolkit. Does this have something that can help...
An Example will be great
Thanks... :)
Edit
My TreeView in a UserControl I made a dependency property in the UserControl which catches the selected Item fo the tree view so that i can use this dependency property to populate the required view in the "MainView" ContentControl binding....as you advised me in the comments. Everything is till now
Problem is that i want to display data contained in the the selected item and i cannot set the DataContext of the UserControls(which will be displayed in right hand side) to the selected item as then i will not be able to use my view model for the respective usercontrol for commands and other operations
I tried to solve this too.... i used the Mediator (Messenger) in my TreeViewUserControl view model to send a Message to the Usercontrol's(the one that i need to display) view model . Message will be passed whenever the item is selected in the tree view. and message contains the selected node. I forgot to mention i set the datacontext of the UserControl to its view model so that i could display data
But using this approach the problem is that when the I click a type of node for the first time the data is not populated but if the same type of node is clicked again its populated. What’s happening is that UserControls object is availabe when the the tree item is clicked for the first time and Mediator sends the message. So Mediator is not able to pass the message to the userControl view model.....
I totally do not have ne idea to solve this further.... is my way if displaying user control right or I should do something that else....totally confused.....
You could try defining a DataTemplate for each type in the TreeView's ItemsSource and instead of having a specific UserControl on the right side, just bind to the TreeView's SelectedItem. Alternatively, you could use a DataTemplateSelector.
Edited for OP's Edit
Did you do this?
MainWindow has TreeView whose ItemsSource=Binding MainVM.Items.
MainWindow has ContentControl whose Content=Binding TreeView.SelectedItem.
Somewhere in project, have ResourceDictionary where each possible type in MainVM.Items has a DataTemplate defined?
Which ViewModel (MainVM or ItemVM) are you trying to use and why can't you use it?

WPF patterns: Items and ItemsSource

Well I'm designing a Custom WPF control - fore the sake of learning - that display logs message in a similar way Visual Studio does. I want to allow the user add messages by adding message istances to an Items collection, or by binding to an ItemSource. I think this is a well established pattern in many wpf controls, but I have no Idea on how achieve it. I know I can obtain the same result by adding a listview as a part of my control, but the project goal is learning, so I prefer avoid that solution. Any idea ?
Have a read around the ItemsControl, your custom control can inherit from an ItemsControl, or a derivative of it. If you create an ObservableCollection containing your items and bind that to your ItemsSource, then your list will be automatically updated. You can style the ItemTemplate and Template to give the list a different look and feel.
There's loads of info here

SL4 data binding syntax issue (debugging declarative databinding)

I'm using the DevExpress GridControl with a context menu. Their context menu implementation uses their bars control which creates a lose reference from a row in the grid to a group of reusable bars. What this means is the context menu items don't have the same context as the grid (sparing details).
I can bind the command (ala ICommand) property of the bar to a command I've creaed on my view model. The challenge is I can't get my syntax to work just right. The VM is set to the DataContext of the view, but something like this isn't workign (where the view's x:name is "VideoModulePage):
Command="{Binding Path=DataContext.ViewVideoCommand, ElementName=VideoModulePage}"
When I create this databinding expression in Blend/VS2010, it makes me thing this is valid, but the command isn't being fired. The CanExecute always returns true. Is this syntax valid? Any tips on a better approach to this? I think what I really need is the new SL5 databinding debugging now! :(
Here's a post I put in the DevExpress forum, but I don't think my issue is with their control, rather with my databinding syntax: http://community.devexpress.com/forums/t/96068.aspx

WPF: How to dynamically find a usercontrol or datatemplate

I have a bunch of datatemplates I use to display various sql-views in an ItemsControl. I don't know which datatemplate i'm going to use until run-time. (every view has different columns)
Next to that, I made a generic dynamic datatemplate for all those views that don't need anything special.
When I display the view I want to first look in all the available datatemplates if there is one that matches, else use the default dynamic datatemplate.
My question is how can I 'search' a datatemplate by name in code? Usercontrol is also possible.
Thanks,
Elger
Yep got it:
Use a datatemplateselector or just use window.FindResource to do the trick.
This helped me:
http://www.developingfor.net/net/dynamically-switch-wpf-datatemplate.html

Binding a WPFToolkit DataGrid's event to a ModelView's Command (MVVM)

In particular I would like to know how to bind the 'SelectionChanged' event of the dataGrid to a Command on my Viewmodel.
Since DataGrid doesnt have a Command property, how do I call a modelView command as in MVVM fashion? I dont mind using a delegate on the code behind XAML, if I knew how to do that...
Since I am new to WPF I am very stuck on how to accomplish this. May someone please help me with this?
Kind Regards,
Kave
Thanks Cameron. I had discovered it first too, but I prefer not using 3rd Party libraries at this stage.
After many many hours, I have found this link that helped me solving the problem in a different way.
In fact, there is no need for a DataGrid to use commands because its not really executing an action such as a button. The 'SelectionChanged' event can be easily made talking to the modelview by exposing a "selectedItem" property in the modelView and bind it to the Datagrid's selectedItem. The following example does it with a combobox instead of a datagrid, but its exactly the same concept. However I recommend using the CollectionView instead and making sure to feed the datagrid with a ObservableCollection<> and not with a e.g. DataTable.
What is the easiest way to handle SelectedItem event with MVVM?

Resources