gpt4 book ai didi

Silverlight TabItem 可见性没有改变

转载 作者:行者123 更新时间:2023-12-04 05:53:09 27 4
gpt4 key购买 nike

我有一个 TabControl,其中有许多 TabItems 绑定(bind)到一个 ViewModel,该 ViewModel 具有每个 TabItem 的可见性的属性。

<sdk:TabControl>
<sdk:TabItem Name="Inventory" Header="Inventory"
Style="{StaticResource TabItemStyle}"
Visibility="{Binding Permissions.Inventory,
Converter={StaticResource PermissiveVisibilityConverter},
ConverterParameter='Viewer'}"
DataContext="{Binding VM}" />

</sdk:TabControl>

所有 TabItems 默认为折叠的可见性。但是,当 VM 将 TabItem 更改为 Visible 时,它​​不会工作,直到您将鼠标移到控件上...

即使我使用按钮以编程方式设置可见性,它的行为方式也是一样的!

我检查了 VM 的属性是否正在通知 UI,并且它们与 NotifyOnPropertyChanged 一起使用。如果我将数据绑定(bind)到按钮的可见性,它就可以正常工作......只是 TabItems 似乎有一个错误。

有没有办法让 TabItem UI 刷新?或者解决这个问题?

谢谢!

最佳答案

我遇到了同样的问题,我能够使用附加的“可见性”属性而不是原始属性来克服它。在这个新属性中,我可以将值传递给原始的“Visibility”属性,如果父选项卡控件的“SelectedItem”被折叠,请选择下一个可见的 tabItem。

但是,如 here 所述,仅当 TabControl 加载时第一项折叠时,这可能还不够。这种情况必须在 TabControl 本身中修复,因为测试显示,当第一次设置假的“可见性”时,TabItem 还没有访问它的 TabControl。因此,我还使用了 TabControl 的附加属性来纠正此问题。

完整的解决方案:

public static class TabControlExtensions
{
/// <summary>
/// Use this property on a TabControl to correct the behavior
/// of selecting Collapsed TabItems.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static bool GetSelectOnlyVisibleTabs(DependencyObject obj)
{
return (bool)obj.GetValue(SelectOnlyVisibleTabsProperty);
}
public static void SetSelectOnlyVisibleTabs(DependencyObject obj, bool value)
{
obj.SetValue(SelectOnlyVisibleTabsProperty, value);
}
public static readonly DependencyProperty SelectOnlyVisibleTabsProperty =
DependencyProperty.RegisterAttached("SelectOnlyVisibleTabs", typeof(bool), typeof(TabControlExtensions), new PropertyMetadata(false, SelectOnlyVisibleTabsChanged));
public static void SelectOnlyVisibleTabsChanged(object sender, DependencyPropertyChangedEventArgs args)
{
var tabControl = sender as TabControl;
if (tabControl == null) return;

if ((bool)args.NewValue)
{
tabControl.SelectionChanged += TabControl_SelectionChanged;
CorrectSelection(tabControl);
}
else
{
tabControl.SelectionChanged -= TabControl_SelectionChanged;
}
}

private static void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs args)
{
var tabControl = sender as TabControl;
if (tabControl == null) return;

CorrectSelection(tabControl);
}

public static void CorrectSelection(TabControl tabControl)
{
var selected = tabControl.SelectedItem as UIElement;
if (selected == null) return;

// If the selected element is not suposed to be visible,
// selects the next visible element
if (selected.Visibility == System.Windows.Visibility.Collapsed)
tabControl.SelectedItem = tabControl.Items.OfType<UIElement>()
.Where(e => e.Visibility == System.Windows.Visibility.Visible)
.FirstOrDefault();
}
}

public static class TabItemExtensions
{
/// <summary>
/// Use this property in a TabItem instead of the original "Visibility" to
/// correct the behavior of a TabControl when a TabItem's Visibility changes.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Visibility GetVisibility(DependencyObject obj)
{
return (Visibility)obj.GetValue(VisibilityProperty);
}
public static void SetVisibility(DependencyObject obj, Visibility value)
{
obj.SetValue(VisibilityProperty, value);
}
public static readonly DependencyProperty VisibilityProperty =
DependencyProperty.RegisterAttached("Visibility", typeof(Visibility), typeof(TabItemExtensions), new PropertyMetadata(Visibility.Visible, VisibilityChanged));

public static void VisibilityChanged(object sender, DependencyPropertyChangedEventArgs args)
{
var tabItem = sender as TabItem;
if (tabItem == null) return;

var visibility = (Visibility)args.NewValue;
if (tabItem.Visibility == visibility) return;

tabItem.Visibility = visibility;
if (visibility == Visibility.Visible) return;

// Finds the tab's parent tabcontrol and corrects the selected item,
// if necessary.
var tabControl = tabItem.Ancestors().OfType<TabControl>().FirstOrDefault();
if (tabControl == null) return;

TabControlExtensions.CorrectSelection(tabControl);
}
}

用法:
<sdk:TabControl local:TabControlExtensions.SelectOnlyVisibleTabs="True">
<sdk:TabItem Header="tabItem1" Visibility="Collapsed">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="TabItem1 which should not be visible (1)" />
</sdk:TabItem>
<sdk:TabItem Header="tabItem2">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="TabItem2 which should be visible (2)" />
</sdk:TabItem>
<sdk:TabItem DataContext="{Binding ViewModel}"
Header="tabItem3"
local:TabItemExtensions.Visibility="{Binding MyProperty,
Converter={StaticResource BoolToVisibilityConverter}}">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="TabItem with binded Visibility (3)" />
</sdk:TabItem>
</sdk:TabControl>

关于Silverlight TabItem 可见性没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9825622/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com