0

我正在尝试在 XAML 中执行此操作:

TextBox tb = (TextBox)e.Item.FindControl("tvNote");

我在滚动查看器中有一个文本框,我需要找到它的值。这是我的代码:

         <Grid x:Name="itemDetailGrid" Margin="0,60,0,50">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <Image Grid.Row="1" Margin="0,0,20,0" Width="180" Height="180" Source="{Binding Image}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}" Visibility="Collapsed" />
            <StackPanel x:Name="itemDetailTitlePanel" Grid.Row="1" Grid.Column="1">
                <TextBlock x:Name="itemTitle" Margin="0,-10,0,0" Text="{Binding Name}" Style="{StaticResource SubheaderTextStyle}"/>
                <TextBlock x:Name="itemSubtitle" Margin="0,0,0,20" Text="{Binding Description}" Style="{StaticResource SubtitleTextStyle}"/>

                <TextBlock Grid.Row="2" Grid.ColumnSpan="2" Margin="0,20,0,0" Text="{Binding Price, Mode=TwoWay,Converter={StaticResource PriceValueConverter}}"  Style="{StaticResource BodyTextStyle}"/>
                <TextBox x:Name="tvQuantity"/>
                <TextBox x:Name="tvNotes"/>
                <Button x:Name="btnAdd" Content="Add" Click="btnAdd_Click"/>
            </StackPanel>
        </Grid>
    </ScrollViewer>

使用按钮单击,我如何找到 TV Note?这是我的按钮点击:

public void btnAdd_Click(object sender, RoutedEventArgs e)
    {
        Button btnAdd = (Button)sender;

        int quan = 0;
        bool q = int.TryParse(tvQuantity.Text, out quan);
        if (q == false)
        {
            MessageDialog messageDialog = new MessageDialog("Please enter a valid quantity.", "Quantity Invalid");
            messageDialog.ShowAsync();
            return;
        }

        //this causes an issue for items in an existing order

        Product product = btnAdd.CommandParameter as Product;
        IEnumerable<OrderDetail> query = App.CurrentOrder.OrderDetails.Where(a => a.Name == product.Name);

        if (query.Count() == 0)
        {

            //insert order
            Repository.AddOrderDetail(product.Name, product.Description, product.Price, quan, App.CurrentOrder.Id, tvNote.Text, product.Category);
            MessageDialog messageDialog = new MessageDialog(product.Name + " has been successfully added to your order.", "Item Added");
            messageDialog.ShowAsync();
            messageDialog.Commands.Add(new UICommand("OK", new UICommandInvokedHandler(YesCommandInvokedHandler)));


        }
        else
        {
            OrderDetail orderDetail = btnAdd.CommandParameter as OrderDetail;
            orderDetail.Quantity = quan;
            orderDetail.OrderDetailNote = tvNote.Text;
            Repository.UpdateOrderDetail(orderDetail.Id, orderDetail.Category, orderDetail.Description, orderDetail.Name, orderDetail.OrderDetailNote, orderDetail.OrderId, orderDetail.Price, quan);
            MessageDialog messageDialog = new MessageDialog(orderDetail.Name + " has been successfully updated.", "Item Updated");
            messageDialog.ShowAsync();
        }
4

1 回答 1

1

我在您的 XAML 中没有看到 tvNote?我懂了:

<TextBox x:Name="tvNotes"/>

所以,也许这是一个错字?

如果这是在 DataTemplate 中,那么您需要切换到使用不同的技术,因为 FindName 不起作用,如此处所示

于 2012-09-08T01:53:34.980 回答