【问题标题】:The name 'xxx' does not exist in the current context in WP7WP7 的当前上下文中不存在名称“xxx”
【发布时间】:2013-01-21 16:33:48
【问题描述】:

这是我的 xaml 代码:

<TextBox x:Name="name_box_det" Text="{Binding Name}" Height="65" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667"/>

这是 C# 代码,用于获取文本值:

var name_text_det = name_box_det.Text;

我得到了这个例外:

The name 'name_box_det' does not exist in the current context

Xaml 代码是从另一个 xaml 复制的,但我尝试编写全新的代码,但没有帮助。你知道错误在哪里吗?


这是完整的 XAML 代码:

<Grid x:Name="LayoutRoot" Background="Transparent">
<Image Margin="0" Grid.Row="1" Source="devdesk.png" Stretch="Fill"/>
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,45,12,12"Orientation="Horizontal">
<ListBox x:Name="listBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Height="27" Margin="0,0,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Record index:" VerticalAlignment="Top" Foreground="#FF6C6C6C"/>
<TextBox Text="{Binding Index}" x:Name="index_box_det" Height="65" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667"/>

                        <TextBlock Name="record_name" Height="27" Margin="0,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Record name:" VerticalAlignment="Top" Foreground="#FF6C6C6C"/>
                        <TextBox x:Name="name_box_det" Text="{Binding Name}" Height="65" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667"/>
                    <TextBlock Height="27" Margin="0,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Beneficiary:" VerticalAlignment="Top" Foreground="#FF6C6C6C"/>
                        <TextBox x:Name="beneficiary_box_det" Text="{Binding Beneficiary}" Height="65" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667"/>
                    <TextBlock Height="27" Margin="0,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Price:" VerticalAlignment="Top" Foreground="#FF6C6C6C"/>
                        <TextBox x:Name="price_box_det" Height="65" Text="{Binding Price}" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667"/>
                    <TextBlock Margin="0,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Deadline:" Foreground="#FF6C6C6C"/>
                        <TextBox x:Name="deadline_box_det" Text="{Binding Deadline}" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667"/>
                    <TextBlock Margin="0,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Description:" Foreground="#FF6C6C6C" Height="27" VerticalAlignment="Bottom"/>
                        <TextBox x:Name="description_box_det" Text="{Binding Description}" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667" Height="285" VerticalAlignment="Bottom"/>
                    <Button Width="375" x:Name="edtbtn" Content="Edit this record" Click="edtbtn_Click" Height="88" Margin="-12,-10,0,0" Grid.Row="1" VerticalAlignment="Bottom" HorizontalAlignment="Left" BorderThickness="3" Foreground="#FF40AA2F" BorderBrush="#FF40AA2F" Background="{x:Null}"/>
                        <Button Width="100" x:Name="dltbtn" Click="dltbtn_Click" Height="88" Margin="-12,-98,0,0" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Bottom" BorderThickness="3" Foreground="#FF40AA2F" BorderBrush="#FF40AA2F" Background="{x:Null}">
                            <StackPanel>
                                <Image Source="delete.png"/>
                            </StackPanel>
                        </Button>

    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
    </ListBox>

    </StackPanel>
</Grid>

【问题讨论】:

  • 当您只显示代码而不是代码所在的上下文时,这很难回答,尤其是当它说这是一个特定于上下文的问题时。
  • 你在哪里设置name_box_det变量?
  • 我的猜测是您的文本框存储在一个控件组内,如 gridview 或中继器,因此您将无法通过名称直接访问它。更多代码将有助于诊断此问题。
  • 你做错了,只要使用“名称”变量,它就被绑定了。在 WPF 中,您几乎不需要在代码隐藏中查找控件。

标签: c# xaml windows-phone-7 datacontext


【解决方案1】:

name_box_det 存在于 ListBox 的 ItemTemplate 中。这是与您的页面不同的上下文,因此是错误。因为集合中的每个项目都存在此 TextBlock 的实例,所以您无法知道您在后面的代码中指的是哪一个。

我猜你正在删除按钮单击事件处理程序中执行此操作。由于您没有提供您正在做的事情的完整重现,这里是一个如何完成的示例。

假设 UI 包含:

<ListBox ItemsSource="{Binding}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Name}" />
                <Button Content="delete" Grid.Column="1" Click="DeleteClicked"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

DataContext 类似于:

this.DataContext = new[]
                        {
                            new SimpleViewModel { Name = "one" },
                            new SimpleViewModel { Name = "two" },
                            new SimpleViewModel { Name = "three" },
                        };

那么点击事件处理程序可以如下所示:

private void DeleteClicked(object sender, RoutedEventArgs e)
{
    MessageBox.Show(
        "You're trying to delete " + 
        ((sender as FrameworkElement).DataContext as SimpleViewModel).Name);
}

【讨论】:

    猜你喜欢
    • 2012-06-21
    • 2010-12-01
    • 2013-10-02
    • 2014-04-22
    • 2017-06-17
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多