网格可见性绑定到TabControl SelectedItem

时间:2013-11-27 13:31:27

标签: c# wpf

我有两个网格包含在这样的边框中:

                    <Border BorderBrush="Gray" BorderThickness="2" Margin="5" Visibility="{Binding SelectedItem, ElementName=tcAction, Converter={StaticResource LoadChangeHeaderToVisibilityConverter}}">
                    <Grid x:Name="actionGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Text=" Card Name" HorizontalAlignment="Center" Margin="0,4,0,0" FontSize="16"/>
                        <Label Grid.Row="1" Content="{Binding CardName}" FontSize="14"  HorizontalAlignment="Center" />
                        <TextBlock Grid.Row="2" Text="{Binding ActionType, StringFormat='Action Type: {0}'}" Margin="4" FontSize="13" FontWeight="ExtraBlack"  HorizontalAlignment="Center"/>
                        <TextBlock Grid.Row="3" Text="{Binding Action, StringFormat='Action: {0}'}"  Margin="4" FontSize="13" FontWeight="ExtraBlack"  HorizontalAlignment="Center"/>
                    </Grid>
                </Border>
                <Border BorderBrush="Gray" BorderThickness="2" Margin="5" Visibility="{Binding SelectedItem, ElementName=tcAction, Converter={StaticResource LoadChangeHeaderToVisibilityConverter}}">
                    <Grid x:Name="actionCANGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Text=" CAN Name" HorizontalAlignment="Center" Margin="0,4,0,0" FontSize="16"/>
                        <Label Grid.Row="1" Content="{Binding CardName}" FontSize="14"  HorizontalAlignment="Center" />
                        <TextBlock Grid.Row="2" Text="{Binding ActionType, StringFormat='Action Type: {0}'}" Margin="4" FontSize="13" FontWeight="ExtraBlack"  HorizontalAlignment="Center"/>
                        <TextBlock Grid.Row="3" Text="{Binding Action, StringFormat='Action: {0}'}"  Margin="4" FontSize="13" FontWeight="ExtraBlack"  HorizontalAlignment="Center"/>
                    </Grid>
                </Border>

它们都绑定到TabControl的SelectedItem属性。这里的事实是,我希望一次只能看到一个网格,具体取决于所选的TabItem。因此,当Visibility对其中一个可见时,它应隐藏给其他人。我不确定如何跟踪所有未来网格的状态,并且只保留前面的1个。

2 个答案:

答案 0 :(得分:1)

就个人而言,我根本不会搞砸Visibility。这是旧的WinForms方式:)

相反,我会使用ContentControl并根据ContentTemplateDataTrigger中切换SelectedItem属性。我发现这更容易维护,并且一次只有一组项目在可视化树中。

<Style TargetType="{x:Type ContentControl}">
    <Setter Property="ContentTemplate" Value="{StaticResource DefaultTemplate}" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=MyTabControl, Path=SelectedIndex}" Value="0">
            <Setter Property="ContentTemplate" Value="{StaticResource ActionTemplate}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding ElementName=MyTabControl, Path=SelectedIndex}" Value="1">
            <Setter Property="ContentTemplate" Value="{StaticResource ActionCANTemplate}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

答案 1 :(得分:0)

而不是绑定tabcontrol的SelectedItem,绑定SelectedIndex并将convertParamereter传递给转换器

您的转换器将如下所示

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((int)value == (int)parameter)
        {
            return Visibility.Visible;
        }
        return Visibility.Collapsed;
    }

在你的Xmal中改变可见性如下

Visibility="{Binding SelectedIndex, ElementName=tcAction, Converter={StaticResource LoadChangeHeaderToVisibilityConverter}, ConverterParameter=0}">