Ask a questionAsk a question
 

AnswerAdding a ViewModel to an ItemControl?

  • Wednesday, November 04, 2009 10:56 PMsaxisa Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Im not doing something right, I want to use an ItemControl to create a grid inside of which I can put a usercontrol.  Ideally, I was hoping to do soemthing like this:
    In the usercontrol resources:
    <DataTemplate DataType="{x:Type vm:PrimerElementViewModel}" >
         <views:PrimerElementView />
    </DataTemplate>

    And then:

    <ItemsControl Grid.Row="2" Margin="5" ItemsSource="{Binding PrimerList}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid Grid.Row="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="{Binding LeftWidth}" />
                        <ColumnDefinition Width="{Binding RightWidth}" />
                    </Grid.ColumnDefinitions>
                    <viewmodel:PrimerElementViewModel />
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

    Where then the view for PrimerElementViewModel would be generated, and know about the dependency properties in my PrimerElement class (in addition to the LeftWidth, RightWidth).

    But this doesnt even compile, because I cannot add a viewmodel to a UIElementCollection.

    Is it possible to do what I want?  How?

Answers

  • Wednesday, November 04, 2009 11:37 PMdmikon Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    You mean in the codebehind constructor?  If I do this, can I still using bindings to the viewmodel from the view?
    Yep. I usually do something like this in a view's constructor:

    private readonly MyViewModel _VM;
    
    public MyView()
    {
        // instantiate the ViewModel
        _VM = new MyViewModel();
       
        // set DataContext
        this.DataContext = _VM;
    }
    

    Whatever bindings you have in 'MyView' (in my example), will reference the underlying ViewModel as its DataContext.

    If you define something in XAML, the no-parameter constructor of that object will get called. That is essentially how things declared in XAML get instantiated.
    • Marked As Answer bysaxisa Thursday, November 05, 2009 12:16 AM
    •  

All Replies

  • Wednesday, November 04, 2009 11:08 PMdmikon Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Why not have your PrimerElementView instantiate its PrimerElementViewModel? Then you can just use the view directly.
  • Wednesday, November 04, 2009 11:29 PMsaxisa Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Why not have your PrimerElementView instantiate its PrimerElementViewModel? Then you can just use the view directly.
    You mean in the codebehind constructor?  If I do this, can I still using bindings to the viewmodel from the view?

    I dont actually know what the difference is between letting WPF instantiate my View and ViewModel using DataTemplate, or just doing it myself.

    Is there any real difference?
  • Wednesday, November 04, 2009 11:37 PMdmikon Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    You mean in the codebehind constructor?  If I do this, can I still using bindings to the viewmodel from the view?
    Yep. I usually do something like this in a view's constructor:

    private readonly MyViewModel _VM;
    
    public MyView()
    {
        // instantiate the ViewModel
        _VM = new MyViewModel();
       
        // set DataContext
        this.DataContext = _VM;
    }
    

    Whatever bindings you have in 'MyView' (in my example), will reference the underlying ViewModel as its DataContext.

    If you define something in XAML, the no-parameter constructor of that object will get called. That is essentially how things declared in XAML get instantiated.
    • Marked As Answer bysaxisa Thursday, November 05, 2009 12:16 AM
    •