我有一個ItemsControl中的超鏈接列表,我希望用戶能夠在超鏈接有焦點時按下空格鍵來調用超鏈接的命令(目前按下enter鍵也可以做到這一點 - 但它不是那麼可發現的IMO) 。這是我的ItemsControl decl:如何讓WPF ItemsControl項目在按空格鍵時觸發命令?
<ItemsControl x:Uid="_commandsItemsControl" Name="_commandsItemsControl" Grid.Row="1" Grid.Column="2" Grid.RowSpan="4" ItemsSource="{Binding Commands}" Background="Transparent"
ScrollViewer.CanContentScroll="False" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden"
HorizontalAlignment="Stretch" HorizontalContentAlignment="Right" VerticalAlignment="Bottom" Margin="12,0,12,0" BorderThickness="0">
<ItemsControl.ItemTemplate>
<DataTemplate x:Uid="DataTemplate_1">
<TextBlock x:Uid="TextBlock_1" HorizontalAlignment="Stretch" TextAlignment="Right">
<Hyperlink x:Uid="Hyperlink_1" Command="{Binding Command}">
<Hyperlink.Inlines>
<Run x:Uid="Run_1" Text="{Binding DisplayName, Mode=OneWay}"/>
</Hyperlink.Inlines>
</Hyperlink>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
這樣做的最好方法是什麼?
是的,我在超鏈接元素上使用了KeyUp處理程序,它工作得很好。 – 2010-08-10 16:31:47