WPF: How to dynamically find a usercontrol or datatemplate - wpf

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

Related

Overriding a property within a ControlTemplate

I have a very large ControlTemplate (200+ lines) with many nested controls inside. I need to reuse this template with a small change to one of these nested controls (visibility of a checkbox). Obviously I don't want to copy-paste into a new ControlTemplate and just make that change because I'll double the code base and if I make a common change to the template I'll need to change both.
I've thought of three solutions that seem like they may work, but I don't know enough about XAML (and especially refactoring XAML to know if these are possible)
Extract the common XAML into a base ControlTemplate and 'override' the checkbox visibility in two new ControlTemplates (I place the override in quotations because I'm using C# speak - I've no idea if that makes sense in XAML!)
Gain access to the checkbox (via x:Name or x:Key maybe) from outside of the ControlTemplate definition, then setting the Visibility would be trivial.
Specify some kind of binding on the Visibilty in the ControlTemplate, something like:
<Checkbox Visibility={Binding someNewPropertyOfTheTemplate}/>
(Is this what TemplateBinding is used for?)
Are any of these ideas valid? And if so, which is the most appropriate? (If not - what is the correct way?)
I'm using VS2010 with .Net 4.0.
I would go for #3. But your code is not quite correct. If you want to bind to properties of the actual control, on which template is being applied, you should use TemplateBinding. Suppose you have a custom control with a property ShowCheckboxes. Then in your template you should use
<Checkbox Visibility={TemplateBinding ShowCheckboxes,
Converter={StaticResource BooleanToVisibilityConverter}}/>
Note, you may have to reference or create appropriate converter.
On the other side, if you use MVVM, you may define your control property on the viewmodel class. Then you should use {Binding}.
Also, there is another way to control which templates are applied.
You may extract the template for your subcontrol out of the big template. And copy it, so you have two templates, that differ in the way you need.
Then, in the main template you can set TemplateSelector for your subcontrol to the custom class, that you will implement. Look at the http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector.aspx for more examples.

Expander in DataGrid (Silverlight)

I have a DataGrid and I need to add an Expander control dynamically to group few rows based on some conditions... Can anyone help me in this... I am completely new to Silverlight :(
Found the answer, I have created a separate column for the expander and managed to display it when my condition is satisfied. It is not dynamic though, but solved the purpose. :)
Dynamically--not sure what you want. I've done expanders statically. My two cents: keep it simple and don't try and do too much. If by dynamically you are taking about Master/Details data gridview, that can be done by XAML and loading the gridview with an ObservableCollection class (search the net). Also search for PagedCollectionView and .Visibility properties for controls. If you want to add controls to a StackPanel, dynamically, search the net for .Children.Remove methods.
Good luck, but as a beginner you are probably trying to do too much IMO.

datarepeater like control in Silverlight?

I need some kind of control to wrap my UI (which generates using binding). Currently I use ListBox but not sure if it's lightest or best choice. I just need placeholder that I can bind to and insert my controls.
You're probably looking for the ItemsControl
You can use any control that takes a list of entities as it's data source.
This could be DataGrid, ListBox (as you are already using) or anything that inherits from ItemsControl - this includes things like the TabControl as well.

Simple WPF ItemsSource Control

This feels like a stupid question, but is there a simple control for WPF that just displays a collection of items? I am currently using a ListBox to display a collection of usercontrols, but the selection element is not needed and the highlighting is actually a distraction. I could disable the highlighting, but this seems like extra work if a simpler control exists. I don't need to track the selected item.
Basically, I want a stackpanel that I can just define an itemssource of viewmodels for. Does such a thing exist?
You can use an ItemsControl - it's pretty much exactly what you are looking for

Problem binding ICommand to event using ElementName within Pivot control

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?

Resources