I have a data driven Silverlight 4 business application with a fairly standard user interface. There's a side section that allows you to enter your search criteria, a standard Silverlight 4 datagrid control in another section that contains your search results (if any), and then a "details" section of the screen which shows the individual information of a single row of the grid when you click on it.
Just underneath my grid control, I have placed a Silverlight DataPager control. When my datagrid has databound search results, I want the DataPager control to be activated that lets you move forwards and backwards through the dataset.
I've got the whole user interface xaml page bound to a custom viewmodel class.
My viewmodel class has a public ObservableCollection property called "Applications". I then set the xaml of my datagrid control to bind to my Applications property:
{datagrid:DataGrid x:Name="grid1" ItemsSource="{Binding Applications}"}
The datagrid control binds to my viewmodel with no issues. However, I'm unable to find the correct xaml syntax to bind the DataPager control to point to my same viewmodel Applications property. So the end result is my DataPager control never activates and remains disabled.
I'm sure I'm missing something obvious, but hoping someone can send me a quick solution.
thanks in advance,
John
Turns out that the xaml for the
DataPager control needed to point to my datagrid control and the binding path to
ItemsSource:
Related
I have used exprssion blend4 edit itemcontainerstyle and increased 3 textblocks in item. NOW, i want to bind the 3 textblocks to a class. The class has 3 different properties. For example,i has a student class,student has name ,age,and major.I want to bind whole school students to listbox and each textblocks display specific information.
You should read at least the MSDN Data Binding Overview. It provides the necessary basic knowledge about data binding in WPF.
I have a tab control where each TabItem is a UserControl. I'd like to hold the UserControls in the TabControl's ItemsSource. Does ItemsSource list go in the Window's ViewModel? If so, I feel like it's breaking MVVM since the ViewModel would now have GUI controls within it. Or do I put this list in the codebehind of the window that holds the tab control?
Any suggestions would be great!
With tab controls, more often than not the individual tabs are created statically in XAML rather than at run time by data binding. However there is no reason why you shouldn't do this. If you have a collection of views, they should definitely be stored in a view.
Bear in mind that you could also bind the ItemsSource to a list of ViewModels objects and WPF will generate a view for you with the ItemTemplate, with the ViewModel object set as the DataContext. This collection of ViewModels should be stored in a view model, although at some point a view model will obviously have to be stored in a view.
This can most likely be done in a number of ways, all which are up for debate on how "MVVM-friendly" they are.
My setup looks like the following.
My Main window has a DataContext bound to a MainWindowViewModel which contains an property
public ObservableCollection<Workspace> WorkspaceCollection{get;set;}
MainWindow has a TabControl which ItemsSource is bound to WorkspaceCollection
Workspace are all viewmodels and are bound to different views/usercontrols via DataTemplates
You might have a look at the Write sample application of the WPF Application Framework (WAF). It has a TabControl where each TabItem is a UserControl and it does this by applying the MVVM pattern.
Here's what I've done.
I created an interface that all of my controls implement, IMyAppControl, which has some information such as Title, Description, other metadata.
My Main Window has an ObservableCollection that the tab ItemsSource binds to.
I am constructing my app infra structure, and finding it hard to achieve a very basic behavior - I want to raise events from different user controls in the system and being able to catch those events on some other user controls that listens to them. For example i have a user control that implements a TreeView. I have another user control that implmements a ListView. Now, i want my ListView to listen to the TreeView, and when the selection is changed on the TreeView, i want to repopulate my ListView accordingly.
I also want this to happen even if the ListView is not located within the TreeView on the WPF logical tree.
PLEASE HELP!!
Thanks,
Oran
Use data binding.
If the content of the list view is stored inside the object shown in the tree view you can just bind into the tree SelectedItem property.
Otherwise bind the tree SelectedItem to a property in your view models (or your window!) and in the setter of this property change the list that is bound to the list view ItemSource property.
You can see the technique in this series on my blog the post I linked to is the last post with the code download link, you'll need to read from the beginning of the series if you want the full explanation.
EDIT: Here's how I did it in one project: (the GridView definition removed since it's not relevant here)
<TreeView
Name="FolderTree"
Width="300"
ItemsSource="{Binding Root.SubFolders}"
ItemTemplate="{StaticResource FolderTemplate}"/>
<ListView
Name="FileView"
ItemsSource="{Binding ElementName=FolderTree, Path=SelectedItem.Files}">
</ListView>
The list bound into the tree view's ItemsSource is of objects that have 3 properties: Name (that is bound to a TextBlock in the FolderTemplate), SubFolders (that is likewise bound to the HierarchicalDataTemplate.ItemsSource property) and Files that is bound to the ListView using {Binding ElementName=FolderTree, Path=SelectedItem.Files}
Note that non of the lists are observable collections (because in this project they never change) but are loaded lazily (on-demand) by the properties getters (because in this project they are expensive to load).
This is the point where the added complexity of MVVM (Model-View-ViewModel pattern) can start to pay off. What you need is a publish/subscribe infrastructure, and MVVM Light has that, along with good MVVM structure that doesn't get overly complex. Prism is another good WPF/Silverlight infrastructure foundation with publish and subscribe support.
I have a usercontrol which contains a TreeView control. I am using MVVM pattern.
I want to reuse this user control in different windows, each time binding the usercontrol to a different datacontext.
<UserControl Name="UserControl1".......>
..............
<TreeView ItemSource={Binding ...}...>
<HierarchicalDataTemplate...........\>
</TreeView>
..............
</UserControl>
In window 1, I want to bind a List<ObjectA> to the TreeView.
In Window 2, I want to bind a List<ObjectB> to the TreeView.
Is it possible to write a generic ViewModel for this usercontrol, so that I can bind different Types of data to the TreeView??
In case my question is not understood, please do let me know.
If I am reading this correctly, you have a UserControl which you wish to reuse, setting its DataContext to be different ViewModel's in different parts of your application...
that being so, yes you can certainly specify Lists of different types as an ItemsSource for your TreeView, but:
The list property must be consistently named within each ViewModel
You will need to describe a DataTemplate (or HierarchicalDataTemplate) for each type you expect to pass into the TreeView within your control's xaml
You may find that binding to an ObservableCollection<T> brings greater reward than a List<T> if you wish to add/remove items to/from the collection and hope to see those changes reflected in the UI
Hope this helps :)
I am developing an mvvm app with wpf. A requirement just got added on to block the user from changing tabs if a textbox has text.
What is the best way to do this completely in the viewmodel? I don't know how to block a tabitem because there is no dependencyobject command in the tabcontrol to tie into, do i need to roll my own tabcontrol and build an ICommand around the SelectionChanged event?
Should I simply (eegad..don't say it) put code in the code behind of the view in the SelectionChanged event?
Do I have an alternative that I haven't thought of?
You might consider binding each of the TabItems' IsEnabled property to a property in your ViewModel (e.g. ViewModel.TabsEnabled) and set that property to False when the textbox has text. That way, you'll be able to enable/disable those tabs from your ViewModel without having to have a code behind file for that particular view.
Which means you'll have something like the following in your view (assuming your ViewModel is a static class named ViewModel):
<TabItem IsEnabled="{Binding Source={x:Static local:ViewModel.TabsEnabled}}"/>
Then you just have to set the TabsEnabled property on the ViewModel when one of the textboxes has content; there are a couple of ways to do this, but if they are bound to your ViewModel you should have plenty of opportunities to listen for changes and set TabsEnabled as appropriate.