gpt4 book ai didi

c# - WPF MVVM : ItemTemplate for binding a list of ICommands to a ListBox

转载 作者:太空狗 更新时间:2023-10-29 23:18:45 27 4
gpt4 key购买 nike

在 MVVM 应用程序中,我想动态显示可在运行时更改的功能按钮。从技术上讲,这并不难,在我的 ViewModel 中,我有一个可观察的 RelayCommands 集合:

public ObservableCollection<RelayCommand> CustomCommands {get;set;}

现在在 Xaml 中我可以将一个 ListBox 绑定(bind)到这个集合:

<StackPanel Orientation="Horizontal">
<ListBox ItemsSource="{Binding CustomCommands}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<wpfhlp:RelayButton DataContext="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>

乍一看这似乎是有效的。
我的问题是:tabstop 顺序坏了。我希望用户能够从一个按钮跳到另一个按钮,但 ListBox 不是按钮而是让用户获得焦点,我可以使用箭头键而不是 Tab 在按钮之间进行选择。

我需要 ListBox 绑定(bind)到集合的能力,但我不需要任何其他列表框功能。
我可以使用其他面板代替 ListBox 吗?
或者我可以以某种方式禁用 ListBox 功能,以便它只显示包含的项目而不能在 ListBox 中选择它们吗?

最佳答案

如果您不需要任何 ListBox 功能,请尝试使用基本 ItemsControl 而不是 ListBox:

<StackPanel Orientation="Horizontal">
<ItemsControl ItemsSource="{Binding CustomCommands}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<wpfhlp:RelayButton DataContext="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>

关于c# - WPF MVVM : ItemTemplate for binding a list of ICommands to a ListBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4164487/

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