1

我对 WPF 很陌生,所以轻松一点

我有一个绑定列表框,它反弹到数据源。

我已经为列表框中的项目创建了一个数据模板,并且某些数据列需要可编辑。为了使它们可编辑,我在资源文件的数据模板中创建了一个绑定文本框。

我的问题是。一旦我进入文本框并编辑了数据。然后我如何使用键盘向下箭头到下一行并让它自动将焦点放在下一行的同一个文本框上。

我已经搜索了 2 天,但似乎没有适合我的问题的解决方案

资源文件中的模板;

<DataTemplate x:Key="myTemplate" x:Name="dTemplate">
    <StackPanel Orientation="Horizontal">
        <Label Content="{Binding Path=b2b_StockCode}" Control.Template="{StaticResource lvTemplate}" Width="80"/>
        <Label Content="{Binding Path=b2b_PartNo}" Control.Template="{StaticResource lvTemplate}" Width="70"/>
        <Label Content="{Binding Path=b2b_desc}" Control.Template="{StaticResource lvTemplate}" Width="245"/>
        <TextBox x:Name="liProfileStockQty" HorizontalContentAlignment="Right" Text="{Binding Path=b2b_ProfileStockQty}" Control.Template="{StaticResource lvTextBoxTemplate}" Width="50"/>
        <Label Content="{Binding Path=b2b_StockQty}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
        <Label Content="{Binding Path=b2b_DBCost}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
        <Label Content="{Binding Path=b2b_InternetCost}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
        <Label Content="{Binding Path=b2b_PerCost}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
        <Label Content="{Binding Path=b2b_TotalCost}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>            
        <Separator Width="2"/>
        <Label Content="{Binding Path=b2b_TradePrice}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
        <Label Content="{Binding Path=b2b_PerTrade}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
        <TextBox x:Name="liDiscountTrade" HorizontalContentAlignment="Right" Text="{Binding Path=b2b_DiscountTrade}" Control.Template="{StaticResource lvTextBoxTemplate}" Width="50"/>
    </StackPanel>
</DataTemplate>

我的主要 Xaml 看起来像这样......

<Window x:Class="B2BPricing.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:s="clr-namespace:System;assembly=mscorlib" 
    xmlns:my="clr-namespace:B2BPricing" 
    Title="B2B Pricing"  Height="768" Width="1024" WindowStartupLocation="CenterScreen" 
    WindowStyle="ThreeDBorderWindow" ResizeMode="CanMinimize" Icon="/B2BPricing;component/NewCEF.ico" >
<Window.Resources>        
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ListViewResources.xaml" />                
        </ResourceDictionary.MergedDictionaries>
        <my:B2BPricingDataSet x:Key="b2bPricing" />
        <CollectionViewSource x:Key="b2bPricingViewSource" Source="{Binding Path=B2bPricing, Source={StaticResource b2bPricing}}" />
        <CollectionViewSource x:Key="b2bPricingsupplierViewSource" Source="{Binding Path=B2bPricing_supplier, Source={StaticResource b2bPricingViewSource}}" />            
    </ResourceDictionary>
</Window.Resources>

<Grid DataContext="{StaticResource b2bPricingViewSource}" Name="MainGrid" >
    <Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="836,12,0,0" Name="resultLabel" VerticalAlignment="Top" />
    <Label Content="Stock Code" Height="28" HorizontalAlignment="Left" Margin="13,34,0,0" Name="lblStockCode" VerticalAlignment="Top" />
    <Label Content="Part No." Height="28" HorizontalAlignment="Right" Margin="0,34,864,0" Name="lblPartNo" VerticalAlignment="Top" />
    <Label Content="Description" Height="28" HorizontalAlignment="Right" Margin="0,34,776,0" Name="lblDescription" VerticalAlignment="Top" />
    <Grid HorizontalAlignment="Left" Margin="51,609,0,0" Name="grid2" VerticalAlignment="Top">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Label Content="Stock Code" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Grid.Row="0" Height="23" HorizontalAlignment="Left" Margin="3" Name="b2b_StockCodeTextBox" Text="{Binding Path=b2b_StockCode, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
    </Grid>

    <Grid HorizontalAlignment="Left" Margin="51,644,0,0" Name="grid1" VerticalAlignment="Top">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Label Content="Description" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="3" Name="b2b_descTextBox" Text="{Binding Path=b2b_desc, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="386" />
    </Grid>

    <Grid HorizontalAlignment="Left" Margin="297,609,0,0" Name="grdSupplier" VerticalAlignment="Top" Width="219">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Label Content="Supplier" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Height="22" HorizontalAlignment="Left" Margin="3,4,0,6" Name="tbSupplier" VerticalAlignment="Center" Width="150" Text="159" />
    </Grid>

    <ListBox ItemsSource="{Binding}" ItemTemplate="{StaticResource myTemplate}" Height="542" HorizontalAlignment="Stretch" 
             Margin="0,60,0,0" Name="lbPricing" VerticalAlignment="Top" Width="983" IsManipulationEnabled="True" 
             SelectionMode="Extended" IsSynchronizedWithCurrentItem="True" 
             KeyboardNavigation.TabNavigation="Continue"
             KeyboardNavigation.DirectionalNavigation="Once">
        <ListBox.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <Label Content="{Binding Path=Name}" Padding="4" Background="{StaticResource myGroupingBrush}" Foreground="White">
                        </Label>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListBox.GroupStyle>
    </ListBox>

    <Button Content="_Load" Height="23" HorizontalAlignment="Left" Margin="52,694,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <ProgressBar HorizontalAlignment="Stretch" Margin="230,311,246,385" Name="progressBar1" VerticalAlignment="Stretch" Width="526" Visibility="Visible" IsIndeterminate="False" />        

    <Grid DataContext="{StaticResource b2bPricingsupplierViewSource}" HorizontalAlignment="Left" Margin="12,-3,0,0" Name="grid4" VerticalAlignment="Top">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Label Content="Supplier: " Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" FlowDirection="LeftToRight" />
        <Label Content="{Binding Path=sup_fullname}" Grid.Column="1" Grid.Row="0" Height="28" HorizontalAlignment="Left" Margin="3" Name="sup_fullnameLabel" VerticalAlignment="Center" />            
    </Grid>
    <Button Content="_Show" Height="23" HorizontalAlignment="Left" Margin="156,694,0,0" Name="btnTextBoxContents" VerticalAlignment="Top" Width="75" Click="btnTextBoxContents_Click" />
</Grid>

和 C#:

 private void btnTextBoxContents_Click(object sender, RoutedEventArgs e)
    {
        ListBoxItem item = (lbPricing.SelectedItem as ListBoxItem);
        TextBox tb = (TextBox)lbPricing.ItemsPanel.FindName("liProfileStockQty", lbPricing);
        //TextBox tb = dTemplate.FindName("liProfileStockQty") as TextBox;
        resultLabel.Content = tb.Text;
    }

这不是我尝试过的唯一方法。我也试过复制这样的东西。

foreach (LinePosition item in this.ListLinePositions.Items)
                {
                      CheckBox cb = FindByName("checkedPosition",
                                                (ListViewItem)this.ListLinePositions.ItemContainerGenerator.ContainerFromItem(item)) as CheckBox;    
                } 

但是用我要查找的文本框控件替换该示例复选框

4

0 回答 0