空 ItemsSource 后出现奇怪的 XAML MenuItem DataBound ItemsSource 刷新问题

发布于 2024-08-03 19:22:15 字数 1500 浏览 0 评论 0原文

我有一个 XAML MenuItem DataBound ItemsSource 工作正常,但有一个小问题。当我查看 MenuItem 并且没有数据绑定项时,ParentMenu 将被禁用,并且看起来很好,没有绑定错误(正如预期的,ItemsSource 为空)。如果修改 ObservableCollection 并添加项目,则绑定会刷新并显示 MenuItem。但是,ItemContainerStyle 不会将 MenuItem.Tag 属性更新为数据绑定值。

只要 ItemsSource 永远不会以空开始或永远不会减少为空,MenuItem 就可以正常工作。有人对这样的问题有什么建议吗?我希望 ItemsSource 和 ItemContainerStyle 始终正确刷新,即使 ItemsSource 以空

XAML 启动,如下所示:

<MenuItem x:Name="MenuItem" Header="Menu" ItemsSource="{Binding Source={StaticResource MenuItemViewSource}}" ItemTemplate="{StaticResource MenuDataTemplate}">
    <MenuItem.ItemContainerStyle>
        <Style TargetType="{x:Type MenuItem}">
            <Setter Property="Tag" Value="{Binding Path=ID, Source={StaticResource MenuItemViewSource}}"/>
            <Setter Property="Background" Value="Red"/>
            <Setter Property="Opacity" Value="0.5"/>
            <EventSetter Event="Click" Handler="MenuItem_Click"/>
        </Style>
    </MenuItem.ItemContainerStyle>
    <MenuItem.Style>
        <Style TargetType="{x:Type MenuItem}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Items.Count, RelativeSource={RelativeSource Self}}" Value="0">
                    <Setter Property="IsEnabled" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </MenuItem.Style>
</MenuItem>

I have a XAML MenuItem DataBound ItemsSource that is working fine however there is a small issue. When I view the MenuItem and there is no databound items the ParentMenu will be disabled and it appears fine without binding errors (as expected the ItemsSource is empty). If the ObservableCollection is modified and an item added, the binding refreshes and the MenuItem appears. However the ItemContainerStyle doesn't upodate the MenuItem.Tag property to the databound value.

The MenuItem works fine as long as the ItemsSource never starts as empty or is never reduced to empty. Does anyone have any tips for such an issue? I would like to have the ItemsSource and ItemContainerStyle refresh correctly at all times even when the ItemsSource starts as empty

XAML is shown below:

<MenuItem x:Name="MenuItem" Header="Menu" ItemsSource="{Binding Source={StaticResource MenuItemViewSource}}" ItemTemplate="{StaticResource MenuDataTemplate}">
    <MenuItem.ItemContainerStyle>
        <Style TargetType="{x:Type MenuItem}">
            <Setter Property="Tag" Value="{Binding Path=ID, Source={StaticResource MenuItemViewSource}}"/>
            <Setter Property="Background" Value="Red"/>
            <Setter Property="Opacity" Value="0.5"/>
            <EventSetter Event="Click" Handler="MenuItem_Click"/>
        </Style>
    </MenuItem.ItemContainerStyle>
    <MenuItem.Style>
        <Style TargetType="{x:Type MenuItem}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Items.Count, RelativeSource={RelativeSource Self}}" Value="0">
                    <Setter Property="IsEnabled" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </MenuItem.Style>
</MenuItem>

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

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

发布评论

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

评论(1

凉城 2024-08-10 19:22:16

只是猜测,但是您是否尝试过绑定到基础数据的 ID,而不将绑定的 DataSource 设置为视图源?像这样:

<Setter Property="Tag" Value="{Binding ID}"/>

样式内的绑定应绑定到 MenuItem 本身的 DataContext,即 MenuItem 所代表的项目。像您所做的那样绑定回视图源可能会造成混淆。

Just a guess, but have you tried binding to the ID of the underlying data without setting the binding's DataSource to the view source? Like this:

<Setter Property="Tag" Value="{Binding ID}"/>

A binding like that within a style should bind to the DataContext of the MenuItem itself, which is the item that the MenuItem represents. Binding back to the view source like you're doing might be confusing it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文