2010-11-12 101 views
4

在MVVM应用程序中,我动态地希望显示可在运行时更改的函数的按钮。从技术上讲,这不是那么难,在我的ViewModel我有RelayCommands的一个ObservableCollection:WPF MVVM:用于将ICommand列表绑定到ListBox的ItemTemplate

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

现在XAML中我可以一个ListBox本类别绑定:

<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获取用户焦点的按钮,我可以使用箭头键而不是选项卡在按钮之间进行选择。

我需要列表框的能力绑定到一个集合,但我不需要任何其他的列表框功能。
是否有一些其他面板,而不是ListBox我可以使用?
或者我可以以某种方式禁用列表框功能,所以它只显示包含的项目,而无法在列表框中选择它们?

回答

4

试基地ItemsControl的,而不是一个列表框,如果你不需要任何的列表框的功能:

<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> 
+0

呀,冷静,那正是我需要的!谢谢! – Sam 2010-11-12 13:40:30