Xaml 如何隐藏数据透视项?
我有一个带有Xaml 如何隐藏数据透视项?,xaml,silverlight,windows-phone-7,pivot,Xaml,Silverlight,Windows Phone 7,Pivot,我有一个带有Pivot-控件的页面,在某些情况下,我不想显示特定的PivotItem 将可见性设置为collapsed似乎根本不会影响它 有什么建议吗?您应该能够使用Pivot.Items上的相应收集方法在Pivot中动态删除或添加PivotItems 如果这对您的场景不起作用,请告诉我。您可以从父透视控件中删除透视项 parentPivotControl.Items.Remove(pivotItemToBeRemoved); 将IsLocked属性设置为true将使除当前透视项目外的所有其他
Pivot
-控件的页面,在某些情况下,我不想显示特定的PivotItem
将
可见性设置为collapsed似乎根本不会影响它
有什么建议吗?您应该能够使用Pivot.Items上的相应收集方法在Pivot中动态删除或添加PivotItems
如果这对您的场景不起作用,请告诉我。您可以从父透视控件中删除透视项
parentPivotControl.Items.Remove(pivotItemToBeRemoved);
将IsLocked
属性设置为true将使除当前透视项目外的所有其他透视项目消失。
但这不会隐藏我们选择的某个特定数据透视项。删除数据透视项很容易,但如果您想在以后将其放回,我发现标题会弄乱并开始相互重叠。如果将标题的可见性设置为“折叠”,然后使其再次可见,也会发生这种情况
因此,我通过将每个不需要的数据透视项(及其标题)的不透明度设置为0,解决了我的特殊问题
但是,这会在缺少数据透视项的位置留下间隙
对我来说,间隔不是问题,因为我只想删除数据透视项列表末尾的项,所以在最后一个数据透视项和第一个数据透视项之间有一些空白。问题是,我仍然能够滑动到隐藏的数据透视项。为了解决这个问题,我重写了Pivot.SelectionChanged(),这样每当用户滑动到隐藏的PivotItem时,代码都会移动到下一个项目。我必须在SelectionChanged()中使用DispatchTimer,并从DispatchTimer回调实际移动到下一个数据透视项,因为您必须在UI线程中才能更改PivotItem.SelectedIndex
private void MainPivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
t.Stop(); //t is my DispatchTimer, set to 100ms
if (MainPivot.SelectedIndex >= mFirstHiddenPivotItemIndex)
{
//move to the first or last PivotItem, depending on the current index
if (mCurrentSelectedPivotItemIndex == 0)
mPivotItemToMoveTo = mFirstHiddenPivotItemIndex - 1;
else
mPivotItemToMoveTo = 0;
t.Start();
}
mCurrentSelectedPivotItemIndex = MainPivot.SelectedIndex;
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
MainPivot.SelectedIndex = mPivotItemToMoveTo;
t.Stop();
}
我已经创建了一个用于显示/隐藏透视项的自定义行为
用法:
< i:Interaction.Behaviors>
< common:HideablePivotItemBehavior Visible="{Binding variable}" />
</ i:Interaction.Behaviors >
代码:
//
///允许显示/隐藏透视项的行为`
///
公共类HideablePivotItemBehavior:行为
{
#区域静态场
公共静态只读DependencyProperty VisibleProperty=DependencyProperty.Register(
“可见”,
类型(bool),
类型(HideablePivotItemBehavior),
新PropertyMetadata(真实,可见PropertyChanged));
#端区
#区域字段
私人支点(parentPivot);;
私有数据透视项_数据透视项;
private int_previousPivotItemIndex;
private int_lastPivotItemsCount;
#端区
#区域公共财产
公众视野
{
得到
{
返回(bool)this.GetValue(VisibleProperty);
}
设置
{
this.SetValue(VisibleProperty,value);
}
}
#端区
#区域方法
受保护的覆盖无效附加()
{
base.onatached();
此._pivotItem=AssociatedObject;
}
私有静态void VisiblePropertyChanged(DependencyObject dpObj、DependencyPropertyChangedEventArgs更改)
{
if(change.NewValue.GetType()!=typeof(bool)| | dpObj.GetType()!=typeof(HideablePivotItemBehavior))
{
返回;
}
var行为=(HideablePivotItemBehavior)dpObj;
var pivotItem=行为。\u pivotItem;
//父轴必须在可视化树初始化后分配
if(行为.\u parentPivot==null)
{
行为。_parentPivot=(Pivot)行为。_pivotItem.Parent;
//如果父项为null,则返回
if(行为.\u parentPivot==null)
{
返回;
}
}
var parentPivot=行为。\u parentPivot;
如果(!(bool)change.NewValue)
{
if(parentPivot.Items.Contains(behavior.\u pivotItem))
{
行为._previousPivotItemIndex=parentPivot.Items.IndexOf(pivotItem);
parentPivot.Items.Remove(pivotItem);
行为。_lastPivotItemsCount=parentPivot.Items.Count;
}
}
其他的
{
如果(!parentPivot.Items.Contains(pivotItem))
{
if(行为.\u lastPivotItemsCount>=parentPivot.Items.Count)
{
parentPivot.Items.Insert(行为。\u先前的PivotItemIndex,pivotItem);
}
其他的
{
parentPivot.Items.Add(pivotItem);
}
}
}
}
#端区
}
详细说明添加/删除数据透视项的解决方案,而不是隐藏数据透视项
假设我们希望数据透视项最初是不可见的,并且只出现在某个事件上
mainPivot.Items.Remove(someTab);
再加一次,
if (!mainPivot.Items.Cast<PivotItem>().Any(p => p.Name == "someTab"))
{
mainPivot.Items.Insert(1,someTab);
}
if(!mainprovot.Items.Cast().Any(p=>p.Name==“someTab”))
{
mainprovot.Items.Insert(1,someTab);
}
我使用插入而不是添加来控制选项卡的显示位置。
您必须确保不会两次添加同一选项卡,这就是if语句的原因。非常有效,感谢我使用了以下代码:NameOfPivotControl.Items.Remove(NameOfPivotControl.Items.Single(p=>((PivotItem)p.Name==“NameOfPivotItem”);它不适用于具有复杂标题的透视项(我的意思是当您使用透视项的控件模板时)。复制:这就是为什么给每个数据透视项一个唯一的x:名称。然后,如果将透视项设置为x:Name=“RemoveMe”,则可以转到pivotcontrol.Items.Remove(RemoveMe)的名称;并完成它。NameOfPivotControl.Items.Remove(NameOfPivotControl.Items.Single(p=>((PivotItem)p.Name==“NameOfPivotItem”);这代码很好。但是,我试图删除另一个错误。告诉我一些解决问题的方法。我知道这是一个旧线程,但是…你不能用Dispatcher.BeginInvoke()代替计时器吗?是的!谢谢,当我发布我的答案时,我是WP的新手。使用BeginInvoke更好。非常好!这是电视台
/// <summary>
/// Behavior which enables showing/hiding of a pivot item`
/// </summary>
public class HideablePivotItemBehavior : Behavior<PivotItem>
{
#region Static Fields
public static readonly DependencyProperty VisibleProperty = DependencyProperty.Register(
"Visible",
typeof(bool),
typeof(HideablePivotItemBehavior),
new PropertyMetadata(true, VisiblePropertyChanged));
#endregion
#region Fields
private Pivot _parentPivot;
private PivotItem _pivotItem;
private int _previousPivotItemIndex;
private int _lastPivotItemsCount;
#endregion
#region Public Properties
public bool Visible
{
get
{
return (bool)this.GetValue(VisibleProperty);
}
set
{
this.SetValue(VisibleProperty, value);
}
}
#endregion
#region Methods
protected override void OnAttached()
{
base.OnAttached();
this._pivotItem = AssociatedObject;
}
private static void VisiblePropertyChanged(DependencyObject dpObj, DependencyPropertyChangedEventArgs change)
{
if (change.NewValue.GetType() != typeof(bool) || dpObj.GetType() != typeof(HideablePivotItemBehavior))
{
return;
}
var behavior = (HideablePivotItemBehavior)dpObj;
var pivotItem = behavior._pivotItem;
// Parent pivot has to be assigned after the visual tree is initialized
if (behavior._parentPivot == null)
{
behavior._parentPivot = (Pivot)behavior._pivotItem.Parent;
// if the parent is null return
if (behavior._parentPivot == null)
{
return;
}
}
var parentPivot = behavior._parentPivot;
if (!(bool)change.NewValue)
{
if (parentPivot.Items.Contains(behavior._pivotItem))
{
behavior._previousPivotItemIndex = parentPivot.Items.IndexOf(pivotItem);
parentPivot.Items.Remove(pivotItem);
behavior._lastPivotItemsCount = parentPivot.Items.Count;
}
}
else
{
if (!parentPivot.Items.Contains(pivotItem))
{
if (behavior._lastPivotItemsCount >= parentPivot.Items.Count)
{
parentPivot.Items.Insert(behavior._previousPivotItemIndex, pivotItem);
}
else
{
parentPivot.Items.Add(pivotItem);
}
}
}
}
#endregion
}
mainPivot.Items.Remove(someTab);
if (!mainPivot.Items.Cast<PivotItem>().Any(p => p.Name == "someTab"))
{
mainPivot.Items.Insert(1,someTab);
}