绑定TabItem可见性

时间:2014-11-26 17:13:42

标签: c# wpf mvvm

我试图将我的TabItem折叠或隐藏。我尝试了很多解决方案,没有一个有效。标签项仍然是

如果我可以得到一些指导,请

我试过的一个解决方案

<TabItem >
    <TabItem.Header>
        <StackPanel Visibility="Collapsed">
           <TextBlock Text="Transactions" />
        </StackPanel>
    </TabItem.Header>
    <panes:Transactions />
</TabItem>

private Visibility statementVisibility;  
public Visibility StatementVisibility { get { return statementVisibility; } set { statementVisibility = value; OnPropertyChanged("StatementVisibillity"); } }

已更改&#34;已折叠&#34;到StatementVisibility,仍然没有。

更新: 在四处寻找之后,我发现了TabItems的链接,我认为这可能是一个因素。

Generic.xaml

<ListBox Foreground="#FFF" Name="TabSelector" Grid.Row="2" ItemsSource="{Binding Path=Items, ElementName=Tabs}">
     <ListBox.Background>
           <SolidColorBrush Color="#333"/>
     </ListBox.Background>
     <ListBox.ItemTemplate>
         <DataTemplate>
              <Border BorderThickness="0 0 0 1" SnapsToDevicePixels="False" BorderBrush="#22000000">
                   <TextBlock FontSize="14" Height="30" VerticalAlignment="Center" Margin="0" Padding="6" Text="{Binding Header}"/>
              </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>


<Border Grid.Column="1" Grid.Row="2" Background="White" BorderThickness="0">
    <ContentPresenter Name="PART_TabbedFormPresenter" 
                            Content="{Binding TabbedForm, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type shell:ActionScreenControl}}}" 
                            DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type shell:ActionScreenControl}}}">
        <ContentPresenter.Resources>
            <Style TargetType="TabItem">
                <Setter Property="Visibility" Value="Collapsed"/>
                <Setter Property="BorderThickness" Value="0"/>
            </Style>
        </ContentPresenter.Resources>
    </ContentPresenter>
</Border>

Loanview.xaml.cs

<shell:ActionScreenControl.TabbedForm>
    <TabControl>
        <TabItem......./>
        <TabItem......./>
        <TabItem >
            <TabItem.Header>
                <StackPanel Visibility="Collapsed">
                    <TextBlock Text="Transactions" />
                </StackPanel>
            </TabItem.Header>
            <panes:Transactions />
        </TabItem>
    </TabControl>
</shell:ActionScreenControl.TabbedForm>

2 个答案:

答案 0 :(得分:0)

尝试在实际的Visibility上设置TabItem属性:

<TabControl>
    <TabItem Visibility="Collapsed">
        <TabItem.Header>
            <StackPanel>
                <TextBlock Text="Transactions" />
            </StackPanel>
        </TabItem.Header>
        <panes:Transactions />
    </TabItem>
</TabControl>

啊......你想要数据绑定。然后,您需要使用BooleanToVisibilityConverter元素和bool属性:

<TabItem Visibility="{Binding YourBoolProperty, 
    Converter={StaticResource BooleanToVisibilityConverter}">
    <TabItem.Header>
        <StackPanel>
           <TextBlock Text="Transactions" />
        </StackPanel>
    </TabItem.Header>
    <panes:Transactions />
</TabItem>

请参阅MSDN上的IValueConverter Interface页面,了解如何使用转换器。

答案 1 :(得分:0)

这是来自生产代码,它可以正常工作

<TabItem  Visibility="{Binding Path=MyGabeLib.CurUser.DisplayTSQL, Converter={StaticResource bvc}}">
        <TabItem.Header>
        <TextBlock Style="{StaticResource HeaderTextBlockStyle}">TSQL</TextBlock>
    </TabItem.Header>
    <ScrollViewer VerticalScrollBarVisibility="Visible">
        <TextBox Text="{Binding Path=MyGabeLib.Search.CurrentTSQL, Mode=OneWay}" IsReadOnly="True"
                    TextWrapping="Wrap" FontFamily="Courier New"/>
    </ScrollViewer>
</TabItem>

如果您要返回Visibility,那么您将不需要转换器 尝试使用简单的TextBlock - 我怀疑你有一个datacontext问题