TabControl隐藏/显示网格 [英] TabControl Hide/Show a Grid

查看:75
本文介绍了TabControl隐藏/显示网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在选择Sketchflow tabcontrol中的某个标签时显示网格。  所有其他标签我希望隐藏网格。

I want to make a grid visible when a certain tab in  a Sketchflow tabcontrol is selected.  All other tabs I want the grid to be hidden.

我知道它是这样的吗?  但我无法正确理解语法?

I know it is something like this?  But I can't get the syntax right?

        <Style TargetType="Grid">
          <Setter Property="Visibility" Value="Hidden"/>
          <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=tabcontrol, Path=SelectedIndex}" Value="3">
              <Setter Property="Visibility" Value="Visible"/>
            </DataTrigger>
          </Style.Triggers>
        </Style>

杰夫戴维斯

推荐答案

如果这不起作用,可能是因为SelectedIndex不是依赖属性,所以绑定不会得到值更改时更新。这是一个你可以使用的简单触发器和一个例子。要使用触发器,请在项目选择文件中 - >新建
项,选择触发器,然后将代码复制到其中。使用"新项目"命令将为您的项目添加所需的引用。

If that doesn't work, it is probably because SelectedIndex is not a dependency property, so the binding won't get updated when the value changes. Here is a simple trigger you can use and an example. To use the trigger, in your project pick file -> new item, select trigger, and then copy the code into it. Using the "new item" command will add the needed references to your project.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Interactivity;

namespace TabControlSelectionChangedTrigger
{
	public class TabControlSelectionChangedTrigger : TriggerBase<TabControl>
	{
		public static readonly DependencyProperty SelectedIndexProperty = DependencyProperty.Register("SelectedIndex", typeof(int), typeof(TabControlSelectionChangedTrigger));

		public int SelectedIndex
		{
			get { return (int) this.GetValue(SelectedIndexProperty); }
			set { this.SetValue(SelectedIndexProperty, value); }

		}
		
		protected override void OnAttached()
		{
			base.OnAttached();

			this.AssociatedObject.SelectionChanged += this.AssociatedObject_SelectionChanged;
		}



		protected override void OnDetaching()
		{
			base.OnDetaching();

			this.AssociatedObject.SelectionChanged -= this.AssociatedObject_SelectionChanged;
		}

		private void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if(this.SelectedIndex == this.AssociatedObject.SelectedIndex)
			{
				this.InvokeActions(null);
			}
		
		}
	}
}





<Grid x:Name="LayoutRoot" Background="White">
		<TabControl x:Name="tabControl" Height="125" Margin="66,53,127,0" VerticalAlignment="Top">
			<i:Interaction.Triggers>
				<TabControlSelectionChangedTrigger:TabControlSelectionChangedTrigger SelectedIndex="1">
					<ei:ChangePropertyAction TargetObject="{Binding ElementName=grid}" PropertyName="Visibility">
						<ei:ChangePropertyAction.Value>
							<Visibility>Collapsed</Visibility>
						</ei:ChangePropertyAction.Value>
					</ei:ChangePropertyAction>
				</TabControlSelectionChangedTrigger:TabControlSelectionChangedTrigger>
				<TabControlSelectionChangedTrigger:TabControlSelectionChangedTrigger SelectedIndex="0">
					<ei:ChangePropertyAction TargetObject="{Binding ElementName=grid}" PropertyName="Visibility">
						<ei:ChangePropertyAction.Value>
							<Visibility>Visible</Visibility>
						</ei:ChangePropertyAction.Value>
					</ei:ChangePropertyAction>
				</TabControlSelectionChangedTrigger:TabControlSelectionChangedTrigger>
			</i:Interaction.Triggers>
			
			<TabItem Header="TabItem">
				<Grid Background="#FFE5E5E5"/>
			</TabItem>
			<TabItem Header="TabItem">
				<Grid Background="#FFE5E5E5"/>
			</TabItem>
		</TabControl>
		<Grid x:Name="grid" Height="93" Margin="224,0,203,144" VerticalAlignment="Bottom"  Background="#FF975B5B"/>
	</Grid>


这篇关于TabControl隐藏/显示网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆