views:

274

answers:

1

Hi there,

I am using the tree view control in my silverlight project. I use data binding for binding my model to the tree. This works perfect.

After adding some features to the tree I ran into two problems:

  1. When I change a property on my Model, the tree does not get updated, even after my onproperty changed get called and also my converter is not called?(I need to hide the item when a specific property changes) (Answered)

  2. How do I hide an Item in the tree? (I use Textbox + image as Item template)

  3. Stack panel is hidden, but empty container remains in tree

DataTemplate:

                <common:HierarchicalDataTemplate ItemsSource="{Binding Children}">
                <StackPanel Orientation="Horizontal" Visibility="{Binding IsAvailable, Converter={StaticResource ItemVisibleConverter} ,Mode=TwoWay}"  >
                    <Image Source="{Binding Converter={StaticResource ImageConverter}}"/>
                    <controls:Label Name="myItem" Content="{Binding Description, Converter={StaticResource ItemConverter} ,Mode=TwoWay}" Foreground="Black"/>
                </StackPanel>
            </common:HierarchicalDataTemplate>

Converter: public object Convert(object value, Type targetType, object parameter, ystem.Globalization.CultureInfo culture) { return GetVisibility(value); }

        private Visibility GetVisibility(object value)
    {
        bool IsVisible= (bool)value;
        if (IsAvailableForDownload)
        {
            return Visibility.Visible;
        }
        else
        {
            return Visibility.Collapsed;
        }
    }

Model

    public class MyModel: INotifyPropertyChanged
{
    public bool IsAvailable
    {
        get
        {
            return _IsAvailableForDownload;
        }
        set
        {
            _IsAvailableForDownload = value;
            onPropertyChanged(this, "IsAvailableForDownload");
        }
    }

//Code for on property changed event
}

Regards

A: 
  1. You probably need to make sure that your model implements INotifyPropertyChanged so that the binding system can do its job.

  2. Could you have a property of type Visibility that your item template binds to, or a bool plus a value converter that returns a Visibility value?

    <DataTemplate> <Grid Visibility="{Binding ThisThingsIsVisible}"> <Button Content="{Binding Blah}" /> </Grid> </DataTemplate>

I don't know if this is the recommended way or not - could your bound object not expose hidden items in their collections?

Jeff Wilcox
Hi Jeff, thank you very much. INotify solved my first problem, but I'm still struggling with number two and three. Any advice?
Running into an error, or looking for guidance on adding the visibility into the data object or view model?
Jeff Wilcox