簡體   English   中英

在ScrollViewer Windows 8 Metro中查找控件

[英]Find Control Inside ScrollViewer Windows 8 Metro

我正在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();
        }

在您的XAML中看不到tvNote嗎? 我懂了:

<TextBox x:Name="tvNotes"/>

所以,也許是錯字?

如果這是一個DataTemplate,那么你就需要切換到使用不同的技術,因為FindName將無法正常工作,如圖所示這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM