如何隐藏数据透视项?

发布于 2024-10-30 13:07:44 字数 139 浏览 1 评论 0原文

我有一个带有 Pivot 控件的页面,在某些情况下我不想显示特定的 PivotItem
Visibility 设置为折叠似乎根本不会影响它。

有什么建议吗?

I have a Page with an Pivot-control and in some cases I don't want to show a particular PivotItem.
Setting the Visibility to collapsed doesn't seem to affect it at all.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

往日 2024-11-06 13:07:44

您应该能够通过使用 Pivot.Items 上的相应集合方法在数据透视表中动态删除或添加 PivotItems 。

如果这不适用于您的场景,请告诉我。

you should be able to remove or add PivotItems dynamically in your Pivot by using the respective collection methods on Pivot.Items .

Let me know if this doesn't work for your scenario.

野の 2024-11-06 13:07:44

我创建了一个自定义行为来显示/隐藏枢轴项目

用法:

< i:Interaction.Behaviors>
    < common:HideablePivotItemBehavior Visible="{Binding variable}" />
</ i:Interaction.Behaviors >

代码:

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

I've created a custom behavior for showing/hiding pivot item

Usage:

< i:Interaction.Behaviors>
    < common:HideablePivotItemBehavior Visible="{Binding variable}" />
</ i:Interaction.Behaviors >

Code:

/// <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
}
泅渡 2024-11-06 13:07:44

您可以从父透视控件中删除透视项

parentPivotControl.Items.Remove(pivotItemToBeRemoved);

You can remove the pivot item from the parent pivot control

parentPivotControl.Items.Remove(pivotItemToBeRemoved);
呢古 2024-11-06 13:07:44

删除 PivotItems 很容易,但如果您想稍后将它们放回去,我发现标题会变得混乱并开始相互重叠。如果您将标题的可见性设置为折叠,然后再次使其可见,也会发生这种情况。

因此,我通过将每个不需要的 PivotItem(及其标题)的不透明度设置为 0 来解决我的特定问题。

              PivotItem p = (PivotItem)MainPivot.Items.ToList()[indexToHide];
              p.Opacity = 0;
              ((UIElement)p.Header).Opacity = 0;

但是,这会在丢失的 PivotItem 所在的位置留下间隙。

对我来说,间隙不是问题,因为我只想删除 PivotItemList 末尾的项目,因此最后一个和第一个 PivotItems 之间有一些空白。问题是,我仍然能够滑动到隐藏的 PivotItem。为了解决这个问题,我重写了 Pivot.SelectionChanged() ,以便每当用户滑动到隐藏的 PivotItem 时,代码就会移至下一个项目。我必须在 SelectionChanged() 中使用 DispatchTimer,并实际上从 DispatchTimer 回调移动到下一个 PivotItem,因为您必须在 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();
    }

Removing PivotItems is easy, but if you want to put them back afterwards I've found that the headers get messed up and start overlapping each other. This also happens if you set the Visibility of a header to Collapsed and then later make it Visible again.

So I solved my particular problem by setting the opacity of each unwanted PivotItem (and its header) to 0.

              PivotItem p = (PivotItem)MainPivot.Items.ToList()[indexToHide];
              p.Opacity = 0;
              ((UIElement)p.Header).Opacity = 0;

However, this leaves gaps where the missing PivotItems are.

For me, the gaps were not a problem because I only want to remove items at the end of my PivotItemList, so I get some whitespace between the last and first PivotItems. The problem was, I was still able to swipe to a hidden PivotItem. In order to fix this, I overrode Pivot.SelectionChanged() so that whenever the user swipes to a hidden PivotItem, the code moves on to the next item instead. I had to use a DispatchTimer from within SelectionChanged() and actually move to the next PivotItem from the DispatchTimer callback, since you have to be in the UI thread to change 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();
    }
热鲨 2024-11-06 13:07:44
foreach (PivotItem item in MyPivot.Items.ToList())
{
    if (item.Visibility == Visibility.Collapsed)
        MyPivot.Items.Remove(item);
}
foreach (PivotItem item in MyPivot.Items.ToList())
{
    if (item.Visibility == Visibility.Collapsed)
        MyPivot.Items.Remove(item);
}
终陌 2024-11-06 13:07:44

将 IsLocked 属性设置为 true 将使除当前数据透视项之外的所有其他数据透视项消失。
但这不会隐藏我们选择的一个特定的关键项目。

Setting IsLocked property to true will make all other Pivot items to disappear except the current pivot item.
But this will not hide one particular pivot item of our choice.

一绘本一梦想 2024-11-06 13:07:44

详细说明添加/删除pivotItems而不是隐藏它们的解决方案。

假设我们希望pivotItem 最初是不可见的,并且仅在特定事件上出现。

mainPivot.Items.Remove(someTab);

然后,为了再次添加它,

 if (!mainPivot.Items.Cast<PivotItem>().Any(p => p.Name == "someTab"))
 {
      mainPivot.Items.Insert(1,someTab);
  }  

我使用“插入”而不是“添加”来控制选项卡出现的位置。
您必须确保不会两次添加相同的选项卡,这就是 if 语句的原因。

To elaborate on the solution of adding/removing pivotItems, rather than hiding them.

Let's say we want the pivotItem to be initially invisible, and appear only on a certain event.

mainPivot.Items.Remove(someTab);

Then to add it again,

 if (!mainPivot.Items.Cast<PivotItem>().Any(p => p.Name == "someTab"))
 {
      mainPivot.Items.Insert(1,someTab);
  }  

I've used Insert rather than add to control the position where the tab appears.
You have to ensure you don't add the same tab twice, which is the reason for the if statement.

绝影如岚 2024-11-06 13:07:44

我修改了 Bajena 行为来改进它,解决了重复显示/隐藏时丢失 PivotItem 原始位置的问题以及父透视控件为空(尚未初始化)时的问题。

请注意,此行为必须附加到数据透视表,而不是数据透视表项。

代码:

  public class PivotItemHideableBehavior : Behavior<Pivot>
  {
    private Dictionary<PivotItem, int> DictionaryIndexes { get; set; }

    public static readonly DependencyProperty VisibleProperty = DependencyProperty.Register(
        "Visible",
        typeof(bool),
        typeof(PivotItemHideableBehavior),
        new PropertyMetadata(true, VisiblePropertyChanged));

    public static readonly DependencyProperty PivotItemProperty = DependencyProperty.Register(
        "PivotItem",
        typeof(PivotItem),
        typeof(PivotItemHideableBehavior),
        new PropertyMetadata(null));

    public bool Visible
    {
      get { return (bool)GetValue(VisibleProperty); }
      set { SetValue(VisibleProperty, value); }
    }

    public PivotItem PivotItem
    {
      get { return (PivotItem)GetValue(PivotItemProperty); }
      set { SetValue(PivotItemProperty, value); }
    }

    protected override void OnAttached()
    {
      base.OnAttached();
      AssociatedObject.Loaded += AssociatedObject_Loaded;
    }

    protected override void OnDetaching()
    {
      base.OnDetaching();
      AssociatedObject.Loaded -= AssociatedObject_Loaded;
    }

    private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
    {
      DictionaryIndexes = new Dictionary<PivotItem, int>();
      int index = 0;
      foreach (PivotItem item in AssociatedObject.Items)
        DictionaryIndexes.Add(item, index++);
    }

    private static void VisiblePropertyChanged(DependencyObject dpObj, DependencyPropertyChangedEventArgs change)
    {
      var behavior = (PivotItemHideableBehavior)dpObj;
      var pivot = behavior.AssociatedObject;
      if (!behavior.Visible)
      {
        if (pivot.Items.Contains(behavior.PivotItem))
          pivot.Items.Remove(behavior.PivotItem);
      }
      else if (!pivot.Items.Contains(behavior.PivotItem))
      {
        int index = 0;
        foreach (var item in behavior.DictionaryIndexes)
        {
          if (item.Key == behavior.PivotItem)
            pivot.Items.Insert(index, behavior.PivotItem);
          else if (pivot.Items.Contains(item.Key))
            index++;
        }
      }
    }
  }

 

用法:

  <Interactivity:Interaction.Behaviors>
    <Behaviors:PivotItemHideableBehavior PivotItem="{x:Bind PivotItemName}" Visible="{Binding IsPivotItemVisible}" />
  </Interactivity:Interaction.Behaviors>

I've modified the Bajena behavior to improve it, solving the issue with losing the original position of the PivotItem when showing/hiding repeteadly and the issue when parentpivot control is null (not initialized yet).

Notice that this behavior must be attached to the Pivot, not to the PivotItem.

Code:

  public class PivotItemHideableBehavior : Behavior<Pivot>
  {
    private Dictionary<PivotItem, int> DictionaryIndexes { get; set; }

    public static readonly DependencyProperty VisibleProperty = DependencyProperty.Register(
        "Visible",
        typeof(bool),
        typeof(PivotItemHideableBehavior),
        new PropertyMetadata(true, VisiblePropertyChanged));

    public static readonly DependencyProperty PivotItemProperty = DependencyProperty.Register(
        "PivotItem",
        typeof(PivotItem),
        typeof(PivotItemHideableBehavior),
        new PropertyMetadata(null));

    public bool Visible
    {
      get { return (bool)GetValue(VisibleProperty); }
      set { SetValue(VisibleProperty, value); }
    }

    public PivotItem PivotItem
    {
      get { return (PivotItem)GetValue(PivotItemProperty); }
      set { SetValue(PivotItemProperty, value); }
    }

    protected override void OnAttached()
    {
      base.OnAttached();
      AssociatedObject.Loaded += AssociatedObject_Loaded;
    }

    protected override void OnDetaching()
    {
      base.OnDetaching();
      AssociatedObject.Loaded -= AssociatedObject_Loaded;
    }

    private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
    {
      DictionaryIndexes = new Dictionary<PivotItem, int>();
      int index = 0;
      foreach (PivotItem item in AssociatedObject.Items)
        DictionaryIndexes.Add(item, index++);
    }

    private static void VisiblePropertyChanged(DependencyObject dpObj, DependencyPropertyChangedEventArgs change)
    {
      var behavior = (PivotItemHideableBehavior)dpObj;
      var pivot = behavior.AssociatedObject;
      if (!behavior.Visible)
      {
        if (pivot.Items.Contains(behavior.PivotItem))
          pivot.Items.Remove(behavior.PivotItem);
      }
      else if (!pivot.Items.Contains(behavior.PivotItem))
      {
        int index = 0;
        foreach (var item in behavior.DictionaryIndexes)
        {
          if (item.Key == behavior.PivotItem)
            pivot.Items.Insert(index, behavior.PivotItem);
          else if (pivot.Items.Contains(item.Key))
            index++;
        }
      }
    }
  }

 

Usage:

  <Interactivity:Interaction.Behaviors>
    <Behaviors:PivotItemHideableBehavior PivotItem="{x:Bind PivotItemName}" Visible="{Binding IsPivotItemVisible}" />
  </Interactivity:Interaction.Behaviors>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文