开发者

Dispalying pools depending od parameter in wpf treeview

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" 
                                       Vertica开发者_如何转开发lAlignment="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 ?


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}}" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜