代码之家  ›  专栏  ›  技术社区  ›  Scott Vercuski

使用MVVM模式的WPF中的选项卡顺序和索引

  •  0
  • Scott Vercuski  · 技术社区  · 14 年前

    我在使用MVVM模式通过WPF应用程序上的控件进行切换时遇到问题。我有以下定义树结构的XAML

    <Grid Background="Transparent" Margin="10">
        <TreeView ItemsSource="{Binding FirstLevelNavigableViewModels}" Background="Transparent" 
                  HorizontalContentAlignment="Stretch"
                  HorizontalAlignment="Stretch"
                  BorderThickness="0"
                  ItemContainerStyle="{StaticResource TreeViewItemStyle1}">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type ViewModel:VendorViewModel}" ItemsSource="{Binding Children}">
                    <View:VendorView HorizontalContentAlignment="Stretch" />
                </HierarchicalDataTemplate>
    
                <DataTemplate DataType="{x:Type ViewModel:ProductViewModel}">
                    <View:ProductView HorizontalContentAlignment="Stretch" />
                </DataTemplate>
            </TreeView.Resources>
        </TreeView>
    </Grid>
    

    加载TreeView时,“ProductView”的XAML如下

    <Border Margin="0,2,2,2" CornerRadius="3" Background="#3FC7B299" DockPanel.Dock="Right" HorizontalAlignment="Right" Width="109">
        <StackPanel Orientation="Vertical" Margin="6,4">
            <DockPanel>
                <TextBlock  DockPanel.Dock="Left" FontFamily="Segoe" FontSize="10" FontWeight="Medium"
                            Foreground="Black" Opacity="0.75" 
                            Text="CALC. REG. PRICE"></TextBlock>
                <Button Width="10" Height="10" Background="Transparent" BorderBrush="Transparent" BorderThickness="0" Padding="-4" Margin="0" Command="{Binding UserDefinedRetailPriceCommand}" Visibility="{Binding UserDefinedRetailPriceButtonView}">
                    <Image Width="10" Height="10" Source="/Arhaus.Pricing.Client;component/Styles/Images/error.png"></Image>
                </Button>
            </DockPanel>
            <TextBox    FontFamily="Segoe" FontSize="16" FontWeight="Medium" KeyboardNavigation.IsTabStop="True" KeyboardNavigation.TabIndex="{Binding RegularPriceTabIndex}"
                        Foreground="Black" Opacity="0.9" KeyboardNavigation.TabNavigation="Continue"
                        ebf:LostFocusBehaviour.LostFocusCommand = "{Binding LostFocusSugg}"
                        Text="{Binding NewSuggestedRetailPrice,Converter={StaticResource FormattingConverter}, ConverterParameter=' \{0:C\}', Mode=TwoWay, UpdateSourceTrigger=LostFocus}" Background="#FFE6DED3" BorderBrush="#FFE6DED3" DataContext="{Binding StringFormat=\{0:c\}, NotifyOnValidationError=True}" Padding="0" TabIndex="1"></TextBox>
        </StackPanel>
    </Border>
    

    我将tab index绑定到一个整数,该整数在加载TreeView时不断增加和绑定(即,在加载每个后续模型时,将其设置为tab index 1、2、3等)。

    我希望能够点击tab并跳转到TreeView中的下一个文本框,但是当我点击tab键时,什么都不会发生。我不确定是否正确设置了标签,但我对WPF非常陌生,而且对于在哪里以及如何设置标签以使其正常工作感到茫然。我习惯于WinForms,您只需设置选项卡索引,然后从那里开始。

    感谢您的帮助,我为大代码块道歉。

    1 回复  |  直到 9 年前
        1
  •  1
  •   HCL    14 年前

    我还没有准备好解决方案,但有些想法可能会有所帮助:

    我不知道RegularPriceTabIndex返回了什么,但它可能会从同一索引的每个新TreeView项目开始? 由于重复使用productView,因此多次给出相同的选项卡索引。也许这会导致一个问题。您可以尝试设置 FocusManager.IsFocusScope="true" 对于ProductView。也许这有帮助。

    尝试也设置 Control.IsTabStop="true" 在文本框上。