1

I had a UserControl that accepted several parameters in its constructor. Because I'm now binding to a collection of ViewModel's it seems the best way to load a UserControl based on a ViewModel is with a DataTemplate.

eg.

<DataTemplate x:Key="ContentTemplate" DataType="{x:Type vm:WhiteboardVM}">
   <local:WhiteboardUC  />
</DataTemplate>

However I still need the Usercontrol to have the parameters when it initialises. How can I achieve this? Or what options do I have?

Thanks

Steve
  • 1,584
  • 2
  • 18
  • 32
  • 1
    Do you have to pass the parameters in the constructor? Why can't they just be properties on the UserControl? – Michael Jun 27 '12 at 06:36
  • 1
    The Usercontrol has a gridview with columns that have an order based on one of the parameters. There's also a lot of controls that's visibility is determined by the type of parameter. Though I could have a converter for visibility I can't think of a way to order the columns. – Steve Jun 27 '12 at 06:44
  • 1
    You [can use](http://stackoverflow.com/questions/1083159/calling-a-parametrized-constructor-from-xaml) parametrized constructor in .NET 4.0, but this is really ugly solution IMHO. Consider using dependency properties on your control – dvvrd Jun 27 '12 at 07:09
  • It doesn't work for me anyway (I get the error "Tags of type 'DefTag' are not supported in template sections"). If you know of a good example of creating an enum and bool dependency properties that achieve the same aim as passing a parameter I'd love to know. – Steve Jun 27 '12 at 23:40

1 Answers1

1

The parameterized construction of GUI elements is possible using ObjectDataProvider but dynamic parameter values are not allowed e.g. using the data template below in an ItemsControl.ItemTemplate is not possible if you expect to pass on items specific parameter value! ...

XAML:

<Window x:Class="LDAPAutocomplete.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:LDAPAutocomplete"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <DataTemplate x:Key="MyContentTemplate">
            <DataTemplate.Resources>
                <ObjectDataProvider x:Key="MyUserControlProvider"
                                    ObjectType="{x:Type local:MyButton}">
                    <ObjectDataProvider.ConstructorParameters>
                        <System:String>Test Me</System:String>
                    </ObjectDataProvider.ConstructorParameters>
                </ObjectDataProvider>
            </DataTemplate.Resources>
            <ContentPresenter
                   Content="{Binding
                        Source={StaticResource MyUserControlProvider}}" />
        </DataTemplate>
    </Window.Resources>
    <StackPanel>
        <local:LDAPAutocompleteTextBox/>
        <ContentControl
             ContentTemplate="{StaticResource MyContentTemplate}"
                   Content="123"/>
    </StackPanel>
</Window>

Code Behind:

public class MyButton : Button
{
    public MyButton(string content)
    {
        this.Content = content;
    }
}
WPF-it
  • 19,625
  • 8
  • 55
  • 71