tags:

views:

263

answers:

1

i m using this article http://blogs.msdn.com/delay/archive/2009/09/23/if-it-walks-like-a-duck-and-talks-like-a-duck-it-must-be-a-treegrid-a-simple-xaml-only-treegrid-ui-for-wpf.aspx

to have hierarchical data... i am having treeview which contains grid and that grid contains textboxes...

i just want to hide/show some columns at runtime of the grid in treeview... at any level.. no matter at parent/child...

A: 

One way to do this is to use the ViewModel concept. A ViewModel is a data structure that represents the UI view of the app, it is the model (the data) + information typically needed for the UI.

In your example, you could have a new class that wraps your data (accessible through a property called, say, Model) and has additional properties, like a boolean IsVisible. INotifyPropertyChanged must be implemented by this class

If I copy and modify a template shown in the mentioned article:

   <TreeView.ItemTemplate>

        <!-- Level 0 template leaves space for 2 child "Toggle" levels -->
        <HierarchicalDataTemplate ItemsSource="{Binding Model.SubItems}">
            <Grid Visibility={Binding IsVisible, Converter={StaticResource B2VConverter}}>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition SharedSizeGroup="Task"/>
                    <ColumnDefinition SharedSizeGroup="Toggle"/>
                    <ColumnDefinition SharedSizeGroup="Toggle"/>
                    <ColumnDefinition SharedSizeGroup="Duration"/>
                    <ColumnDefinition SharedSizeGroup="Notes"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding Model.Task}" Style="{StaticResource TextBlockStyle}"/>
                <TextBlock Grid.Column="3" Text="{Binding Model.Duration}" Style="{StaticResource TextBlockStyle}"/>
                <TextBlock Grid.Column="4" Text="{Binding Model.Notes}" Style="{StaticResource TextBlockStyle}"/>
            </Grid>
        ... 

The new things are (I am unable to bold the text I added) the IsVisible property setting at the grid level and the data property references where 'Model.' has been added. It is bound to the new IsVisible property through a converter that you have to declare at the resources level:

Now, you modify your ViewModel when you want to hide or show an item in the tree. As simple as that. This is a rough introduction to the MVVM way of programming. You'll find lots of literature about it. It has quite a few advantages like the capability to unit test the UI logic (i.e. under which circumstances should a tree element become visible ?).

A simpler, less modular way to do what you request is to add the IsVisible property directly to the Model, but this is discouraged as on is mixing up apples and oranges. It may be OK for a very small program that is used for a couple of days and then thrown away.

Timores