C# 绑定到选项卡控件SelectedItem的栅格可见性

C# 绑定到选项卡控件SelectedItem的栅格可见性,c#,wpf,C#,Wpf,边框中包含两个网格,如下所示: <Border BorderBrush="Gray" BorderThickness="2" Margin="5" Visibility="{Binding SelectedItem, ElementName=tcAction, Converter={StaticResource LoadChangeHeaderToVisibilityConverter}}"> <G

边框中包含两个网格,如下所示:

                    <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属性。这里的事实是,我希望一次只能看到一个网格,具体取决于所选的选项卡项。因此,当其中一个对象的可见性可见时,其他对象的可见性应隐藏。我不知道如何跟踪所有未来网格的状态,并且只保留1个在前面

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

您的转换器如下所示

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

就我个人而言,我根本不会影响能见度。这是老办法:)

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

<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>