Reputation: 2754
Okay, so I did a bunch of searching around for this and I either can't seem to wrap my head around the right question, or it just isn't done...
Basically I have a modular application which contains various data-driven 'Applets', which can potentially change and are dynamically added in based upon the permissions that have been provided to the user. I've already built this application in Flex, and we are moving it to WPF through MVVM because it's a million times easier for us to code and maintain.
Now, there are two issues I'm trying to figure out:
Issue #1
I need to be able to dynamically add a single control through binding. I've already built bindings using data templates on ItemsControls that hook up to a list of objects in the ViewModel, and that works wonderfully, but that system seems a little hacky when I know that there will only ever be ONE control but it gets me by for now.
Here is my code for that to show why this seems hacky to me:
XAML
<ItemsControl ItemsSource="{Binding ApplicationList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CTRL:PortalApplicationControl />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
ViewModel
/// <summary>
/// A List to contain the currently active application for binding to the ItemsControl in XAML
/// </summary>
public List<Models.Application> ApplicationList
{
get { return (List<Models.Application>)GetValue(ApplicationListProperty); }
set { SetValue(ApplicationListProperty, value); }
}
public static readonly DependencyProperty ApplicationListProperty =
DependencyProperty.Register("ApplicationList", typeof(List<Models.Application>), typeof(ApplicationVM));
/// <summary>
/// The currently active application
/// </summary>
public Models.Application Application
{
get { return (Models.Application)GetValue(ApplicationProperty); }
set { SetValue(ApplicationProperty, value); ApplicationList = new List<Models.Application>() { Application }; }
}
public static readonly DependencyProperty ApplicationProperty =
DependencyProperty.Register("Application", typeof(Models.Application), typeof(ApplicationVM));
As you can see, it's hacky in the sense that the objects which are binding to properties on the Application dependency property are able to call '{Binding Application.Name}' but the ItemsControl has to call '{Binding ApplicationList}' and the setter for Application has to instantiate a new 'List' of length 1 to set the ApplicationList object in turn....it works, but it's not very elegant and I want to know if there is another way of accomplishing this task
Issue #2
Okay, now we get to the part that throws a monkey wrench in the entire structure above: What if I want different controls in the data template based upon which Application object is currently active? I.E. If Application1 is currently active, use Application1View, but if Application2 is currently active, use Application2View...This seems like I am going to have to do some work in the code behind to get it to work...and that strikes me as a big no no if I'm sticking with the MVVM structure
Sorry for being long winded, I hope that the question makes sense and thanks for any help you guys can offer...this is putting my project at a standstill until I can resolve the above issues
Upvotes: 1
Views: 1434
Reputation: 6014
If there will only be one control at the same time, use the ContentControl.
Upvotes: 1