WPF Treeview, Business Objects and View Models Oh My!

WPF Treeview, Business Objects and View Models Oh My!

Old forum URL: forums.lhotka.net/forums/t/8306.aspx


kevnworking posted on Thursday, January 07, 2010

Hey All,

It's Kevin the WPF newbie back for more.  I apologize ahead of time if this question has more to do with WPF than CSLA but I'm stumped.

I have a set of objects that are laid out like this:
User Object -> User Modules Collection -> User Module Object

I have added some view model classes so I could sync up some of the settings of the treeview items controls, specifically I wanted to add an "IsExpanded" property to the view model class so I could maintain the treeview items current expanded or unexpanded "state" when a save occurred on the underlying business objects.  With some help from Rocky on another thread I was able to put together view model classes for each of the three classes listed above.

In my XAML I have the following lines to hook everything up:
(not sure how to maintain code color)

<!-- View Models -->
<this:UserViewModel x:Key="UserVM" />
<this:UserModuleCollectionViewModel x:Key="UserModuleCollectionVM" Model="{Binding Source={StaticResource UserVM}, Path=Model.UserModules}" />
<this:UserModuleViewModel x:Key="UserModuleVM" Model="{Binding Source={StaticResource UserModuleCollectionVM}, Path=SelectedUserModule}"/>

Further down the page I have the XAML for the treeview:

<TreeView Name="trvModules"
          Grid.Column="0"
          DataContext="{Binding Source = {StaticResource UserModuleCollectionVM}}"
          ItemsSource="{Binding Path=Model}"
          >
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding UserModules}">
            <StackPanel Orientation="Horizontal">
                <CheckBox Margin="1,1,5,1"
                          IsChecked="{Binding Path=HasModuleAccess, Converter={StaticResource IdentityConverter}, Mode=TwoWay}"
                          Checked="chkModules_Checked"
                          Name="chkModules" />
                <TextBlock Style="{DynamicResource TextBlock_MouseOverTrigger_Style}"
                           Text="{Binding Path=DisplayName}"
                           ToolTip="{Binding Path=ModuleDescription}">
                </TextBlock>
            </StackPanel>

            <HierarchicalDataTemplate.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                </Style>
            </HierarchicalDataTemplate.ItemContainerStyle>

            </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

This XAML works great for displaying the data, the treeview is populated with the correct data.  Basically I'm back to where I was before I started working on the view models.

Now, the "
<HierarchicalDataTemplate.ItemContainerStyle>" tag is there just because I was messing around trying to figure this out.  It doesn't work, nor should it because there is no binding to the UserModuleVM object.  Which leads me to ask - how do I implement this so I can bind the UserModuleVM's IsExpanded property to the "IsExpanded" property of the tree view item?

Thanks for any help or advice you can give me...

Kevin

kevnworking replied on Monday, January 11, 2010

Did I now word this very well or is it really a head-scratcher?  I know I'm having a tough time with it!

Kevin

AndresN replied on Wednesday, November 24, 2010

Could there please be a chance that you upload the source you ended up with?

(some compilable project, if possible, or at least the XAML+codebehind+CSLA classes)

I'm a total CSLA newbie, but I'm hoping CSLA solves some complexity I've been fighting almost a year now. And there's WPF TreeView on the UI end.

Please!

Andres

kevnworking replied on Wednesday, November 24, 2010

Andres,

I'm afraid I never did figure this out and sort of "shelved" the project until I have time to work on it again.  The way my workload has been lately, I'm not sure when that's going to happen!

I wish I could help you out.

Kevin

Copyright (c) Marimer LLC