gpt4 book ai didi

wpf - 如何使用 MultiBinding 在上下文菜单中隐藏分隔符?

转载 作者:行者123 更新时间:2023-12-03 01:03:25 26 4
gpt4 key购买 nike

我正在 wpf TreeView 上使用上下文菜单,并且我几乎可以满足我的需求。在解释问题之前,让我先解释一下上下文菜单的 XAML 定义的作用。

对于上下文菜单中的每个菜单项,我们都有一个命令,该命令根据命令 CanExecute 方法禁用或启用菜单项。每个命令都会根据 CanExecute 的结果设置相应菜单项的 IsEnabled 属性。

每个菜单项的 IsEnabled 都绑定(bind)到一个 BooleanToVisibilityConverter,它将 IsEnabled bool 值转换为要绑定(bind)菜单项的 Visibility 属性的 Collapse 或 Visible 值。这又工作得很好,我的菜单项显示和隐藏得很好。

现在来解决这个问题。在下面的 XAML 中,分隔符上方有两个菜单项(addCategoryMenuItem 和 removeCategoryMenuItem)。我正在尝试通过 IMultiValueConverter (MultiBooleanToVisibilityConverter) 的自定义实现对这两个菜单项的 IsEnabled 属性进行多重绑定(bind),以便在禁用这两个菜单项时,我可以将分隔符的可见性属性设置为折叠,从而在菜单项被禁用。

对于我的 Converter(MultiBooleanToVisibilityConverter) 中的 Convert 方法,参数值(对象 [] 值)我在数组中得到两个包含值“{DependencyProperty.UnsetValue}”的项目。这些不能转换为 bool 值,因此无法计算出我的可见性值。

也许与 MultiBinding 中使用的 ElementName 有关。找不到元素吗?我尝试过使用RelativeSource,即查找祖先等。但我只是感到困惑。我已经花了几个小时在这上面,所以现在我把它留给社区。

亲切的问候

穆罕默德

<ContextMenu x:Key="CategoryMenu">
<ContextMenu.ItemContainerStyle>
<Style TargetType="{x:Type Control}">
<Setter Property="Visibility" Value="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}, Mode=OneWay, Converter={StaticResource booleanToVisibilityConverter}}" />
</Style>
</ContextMenu.ItemContainerStyle>
<ContextMenu.Items>
<MenuItem x:Name="addCategoryMenuItem" Header="add category" Command="{Binding AddCategory}">
<MenuItem.Icon>
<Image Source="/Images/add.png" Width="16" Height="16" />
</MenuItem.Icon>
</MenuItem>
<MenuItem x:Name="removeCategoryMenuItem" Header="remove category" Command="{Binding RemoveCategory}">
<MenuItem.Icon>
<Image Source="/Images/remove.png" Width="16" Height="16" />
</MenuItem.Icon>
</MenuItem>
<Separator>
<Separator.Visibility>
<MultiBinding Converter="{StaticResource multiBooleanToVisibilityConverter}">
<Binding Mode="OneWay" ElementName="addCategoryMenuItem" Path="IsEnabled" />
<Binding Mode="OneWay" ElementName="removeCategoryMenuItem" Path="IsEnabled" />
</MultiBinding>
</Separator.Visibility>
</Separator>
<MenuItem x:Name="refreshCategoryMenuItem" Header="refresh" Command="{Binding RefreshCategory}">
<MenuItem.Icon>
<Image Source="/Images/refresh.png" Width="16" Height="16" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu.Items>
</ContextMenu>

最佳答案

我扩展了普通分隔符来创建一个分隔符,该分隔符可以根据父 ItemsControl 中的其他项目自动确定是否应显示它。

public class AutoVisibilitySeparator : Separator
{
public AutoVisibilitySeparator()
{
if (DesignerProperties.GetIsInDesignMode(this))
return;

Visibility = Visibility.Collapsed; // Starting collapsed so we don't see them disappearing

Loaded += OnLoaded;
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
// We have to wait for all siblings to update their visibility before we update ours.
// This is the best way I've found yet. I tried waiting for the context menu opening or visibility changed, on render and lots of other events
Dispatcher.BeginInvoke(new Action(UpdateVisibility), DispatcherPriority.Render);
}

private void UpdateVisibility()
{
var showSeparator = false;

// Go through each sibling of the parent context menu looking for a visible item before and after this separator
var foundThis = false;
var foundItemBeforeThis = false;
foreach (var visibleItem in ((ItemsControl)Parent).Items.OfType<UIElement>().Where(i => i.Visibility == Visibility.Visible || i == this))
{
if (visibleItem == this)
{
// If there were no visible items prior to this separator then we hide it
if (!foundItemBeforeThis)
break;

foundThis = true;
}
else if (visibleItem is AutoVisibilitySeparator || visibleItem is Separator)
{
// If we already found this separator and this next item is not a visible item we hide this separator
if (foundThis)
break;

foundItemBeforeThis = false; // The current item is a separator so we reset the search for an item
}
else
{
if (foundThis)
{
// We found a visible item after finding this separator so we're done and should show this
showSeparator = true;
break;
}

foundItemBeforeThis = true;
}
}

Visibility = showSeparator ? Visibility.Visible : Visibility.Collapsed;
}
}

关于wpf - 如何使用 MultiBinding 在上下文菜单中隐藏分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5049596/

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