空 ItemsSource 后出现奇怪的 XAML MenuItem DataBound ItemsSource 刷新问题
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是猜测,但是您是否尝试过绑定到基础数据的 ID,而不将绑定的 DataSource 设置为视图源?像这样:
样式内的绑定应绑定到 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:
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.