在ScrollViewer Windows 8 Metro中查找控件

时间:2012-09-07 23:52:48

标签: c# xaml microsoft-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();
        }

1 个答案:

答案 0 :(得分:1)

我的XAML中没有看到tvNote?我明白了:

<TextBox x:Name="tvNotes"/>

那么,也许这是一个错字?

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