简体   繁体   English

WPF。 禁用/启用ListBox项ContextMenu和MVVM

[英]WPF. Disable/Enable ListBox items ContextMenu & MVVM

I have a listbox with several items. 我有一个包含多个项目的列表框。 This items could be in 3 state: 该项目可能处于3种状态:

  • Not Connected 未连接
  • Connected 连接的
  • Active 活性

I need to assign a context menu on each of those items. 我需要在每个项目上分配一个上下文菜单。 Actually, I want to have one list of menu items to reuse for each listbox items and I want to disable/enable menu items according to their state. 实际上,我想要一个菜单​​项列表供每个列表框项重用,并且我想根据其状态禁用/启用菜单项。 I'm using MVVM. 我正在使用MVVM。 I wanted to know what's best practice to achieve my goal? 我想知道实现目标的最佳实践是什么?

    <DataTemplate x:Key="TemplateSelector">
        <ContentPresenter Content="{Binding}" Name="contentPresenter">
            <telerik:RadContextMenu.ContextMenu>
                <telerik:RadContextMenu>
                    <telerik:RadMenuItem Header="Connect" Style="{StaticResource ResourceKey=ContextMenuStyle}" />
                    <telerik:RadMenuItem Header="Disconnect" />
                    <telerik:RadMenuItem Header="Delete Database" />
                </telerik:RadContextMenu>
            </telerik:RadContextMenu.ContextMenu>
        </ContentPresenter>

        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=ConnectionType}" Value="Disconnected">
                <Setter TargetName="contentPresenter" Property="ContentTemplate" Value="{StaticResource OfflineDeviceItemTemlpate}" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=ConnectionType}" Value="Internet">
                <Setter TargetName="contentPresenter" Property="ContentTemplate" Value="{StaticResource OnlineDeviceItemTemplate}" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=ConnectionType}" Value="Wifi">
                <Setter TargetName="contentPresenter" Property="ContentTemplate" Value="{StaticResource OnlineDeviceItemTemplate}" />
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</UserControl.Resources>


<Grid>
    <telerik:RadListBox x:Name="lsbDevices" ItemsSource="{Binding Path=Devices}" ItemTemplate="{StaticResource TemplateSelector}" 
                        SelectedItem="{Binding SelectedDevice, Mode=TwoWay}" Grid.Row="0" />
</Grid>

I'm thinking adding a property for each menu item in ModelView which will state if the corresponding menu item should be enabled or not (For example, public bool ConnectEnabled {...} ). 我正在考虑为ModelView中的每个菜单项添加一个属性,该属性将说明是否应启用相应的菜单项(例如,public bool ConnectEnabled {...} )。 Is this a good approach or I'm doing something very wrong here? 这是一个好方法还是我在这里做错了什么?

Thanks 谢谢

I think this is a UI logic and you should not write it in XAML.. I would suugest you can have a class which gets binded to list box items and this class will have a state object or property which will maintain the enable/disable logic for context menu. 我认为这是一个UI逻辑,您不应该用XAML编写它。.我建议您可以有一个绑定到列表框项目的类,并且该类将具有状态对象或属性,该对象或属性将维护启用/禁用逻辑用于上下文菜单。 You can bind this proeprty to context menu. 您可以将此属性绑定到上下文菜单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM