使用 MVVM 模式的 WPF 中的 Tab 键顺序和索引

发布于 2024-09-17 21:44:42 字数 2934 浏览 1 评论 0原文

我在使用 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>

加载树视图时,“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>

我将选项卡索引绑定到一个整数,该整数在加载树视图时不断增加和绑定(IE 我有当加载每个连续模型时,它设置为选项卡索引 1、2、3 等)。

我希望能够点击选项卡并跳转到树视图中的下一个文本框,但是当我单击 TAB 键时,没有任何反应。我不确定我的选项卡设置是否正确,但我对 WPF 非常陌生,不知道在哪里以及如何设置选项卡以使其正常工作。我习惯了 WinForms,您只需设置选项卡索引并从那里开始。

感谢您的帮助,对于较大的代码块,我深表歉意。

I'm having an issue with tabbing through the controls on a WPF application using the MVVM pattern. I have the following XAML which defines a tree structure

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

When the treeview is loaded, the XAML for the "ProductView" is as follows

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

I have the Tab Index bound to an integer that is ever increasing and bound as the treeview is loaded (I.E. I have it setup as tab index 1, 2, 3 etc. as each successive model is loaded).

I would like to be able to hit tab and jump to the next textbox in the treeview but when I click the TAB key, nothing happens. I'm not sure if I have the tabbing setup correctly but I'm very new to WPF and at a loss as to where and how to set the tabbing to make it work. I'm used to WinForms where you just set the tab index and go from there.

Thank you for your assistance, I apologize for the large code block.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

固执像三岁 2024-09-24 21:44:42

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

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

还尝试设置 Control文本框上的.IsTabStop="true"

I don't have a solution ready but some thoughts that maybe may help:

I don't know what RegularPriceTabIndex returns but probably it begins for each new TreeViewItem at the same index?
The same tab-index is given then multiple times because of repeating use of ProductView. Perhaps this leads to a problem. You can try setting FocusManager.IsFocusScope="true" for the ProductView. Maybe this helps.

Try also to set Control.IsTabStop="true" on the TextBox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文