Sam*_*Sam 5 c# wpf binding listbox mvvm
在MVVM应用程序中,我动态地想要显示可在运行时更改的函数的按钮.从技术上讲,这并不是那么困难,在我的ViewModel中,我有一个可观察的RelayCommands集合:
public ObservableCollection<RelayCommand> CustomCommands {get;set;}
Run Code Online (Sandbox Code Playgroud)
现在在Xaml中我可以将ListBox绑定到此Collection:
<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>
Run Code Online (Sandbox Code Playgroud)
乍一看,这看起来很有效.
我的问题是:tabstop命令被破坏了.我希望用户能够从按钮跳到按钮,但是ListBox不是按钮,而是使用箭头键而不是tab来选择按钮.
我需要ListBox能力绑定到一个集合,但我不需要任何其他的列表框功能.
是否有其他面板而不是我可以使用的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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1514 次 |
| 最近记录: |