tags:

views:

16

answers:

1

Hi,

I've got a wpf treeview control and depending on parameter in constructor, I want to display pool NumberOfHotels or not display.

<Grid>
    <StackPanel Name="stackPanel1">
        <GroupBox Header="Wybierz" 
                  Height="354" 
                  Name="groupBox1" 
                  Width="Auto">
            <TreeView Name="structureTree"
                      SelectedItemChanged="structureTree_SelectedItemChanged"
                      Grid.Row="0" Grid.Column="0" 
                      ItemsSource="{Binding}" 
                      Height="334" Width="Auto"
                      ScrollViewer.VerticalScrollBarVisibility="Visible" 
                      ScrollViewer.HorizontalScrollBarVisibility="Visible" 
                      PreviewMouseRightButtonUp="structureTree_PreviewMouseRightButtonUp" 
                      FontFamily="Verdana" FontSize="12" 
                      BorderThickness="1" MinHeight="0" 
                      Padding="1" Margin="-1"
                      Cursor="Hand">
                <TreeView.Resources>
                    <HierarchicalDataTemplate DataType="{x:Type MyService:Country}" 
                                              ItemsSource="{Binding Path=ListOfRegions}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock TextAlignment="Justify" 
                                       VerticalAlignment="Center" 
                                       Text="{Binding Path=Name}"/>
                            <TextBlock TextAlignment="Justify" 
                                       VerticalAlignment="Center" 
                                       Text=" "/>
                            <TextBlock TextAlignment="Justify" 
                                       VerticalAlignment="Center" 
                                       Text="H:"/>
                            <TextBlock TextAlignment="Justify" 
                                       VerticalAlignment="Center" 
                                       Text="{Binding Path=NumberOfHotels}"/>
                            <TextBlock TextAlignment="Justify" 
                                       VerticalAlignment="Center" 
                                       Text=" "/>
                            <TextBlock TextAlignment="Justify" 
                                       VerticalAlignment="Center" 
                                       Text=" S:"/>
                            <TextBlock TextAlignment="Justify" 
                                       VerticalAlignment="Center" 
                                       Text="{Binding Path=NumberOfZones}"/>
                        </StackPanel>
                    </HierarchicalDataTemplate>
                </TreeView.Resources>
            </TreeView>
        </GroupBox>
    </StackPanel>
</Grid>

Is there any way to do this ?

+1  A: 

Yes. In your constructor set a property of type Visibility like this:

public class MyUserControl : UserControl
{
  public TreeViewVisibility { get; private set; }

  public MyUserControl(bool showTreeView)
  {
    TreeViewVisibility = showTreeView ? Visibility.Visible : Visibility.Collapsed;
    ...
  }
}

And bind to it in your XAML:

...
<TreeView Visibility="{Binding TreeViewVisibility,
           RelativeSource={RelativeSource FindAncestor,local:MyUserControl,1}}" />
Ray Burns