Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 访问列表框中包含的文本块_C#_Visual Studio_Silverlight - Fatal编程技术网

C# 访问列表框中包含的文本块

C# 访问列表框中包含的文本块,c#,visual-studio,silverlight,C#,Visual Studio,Silverlight,我有一个文本块,它在一个列表框中,我正试图编写一个依赖于这个文本块内容的if语句。我试图从我命名为“category1”的文本black中获取数据,但是当我试图编写if语句时,我得到的消息只是 “名称类别1在当前上下文中不存在” 我厌倦了把文本块从列表框中移出,它工作得很好,但是当它在里面的时候就不工作了。有人知道如何引用这个文本块吗 下面是我的XAML代码 <ListBox x:Name="HINList" Margin="0,300,-12,0" ItemsSource

我有一个文本块,它在一个列表框中,我正试图编写一个依赖于这个文本块内容的if语句。我试图从我命名为“category1”的文本black中获取数据,但是当我试图编写if语句时,我得到的消息只是

“名称类别1在当前上下文中不存在”

我厌倦了把文本块从列表框中移出,它工作得很好,但是当它在里面的时候就不工作了。有人知道如何引用这个文本块吗

下面是我的XAML代码

        <ListBox x:Name="HINList" Margin="0,300,-12,0" ItemsSource="{Binding Details}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="0,0,0,17" Width="432">
                        <TextBlock Text="{Binding HINNumber}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                        <TextBlock Text="{Binding CategoryLetter}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
                        <TextBlock x:Name="category1" Text="{Binding Category1}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
                        <TextBlock Text="{Binding Category2}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
                        <TextBlock Text="{Binding Category3}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

假设您正在代码隐藏文件中编写if语句,则不会出现以下情况:

if(((WhateverTypeIsInDetailsCollection)HINList.SelectedItem).Category1 == something) {
     // then do whatever you want
}

正如罗素指出的,列表中的每个条目都有一个类别1项。我假设您希望对所选项目执行某些操作。

假设您正在代码隐藏文件中编写if语句,则不会执行以下操作:

if(((WhateverTypeIsInDetailsCollection)HINList.SelectedItem).Category1 == something) {
     // then do whatever you want
}
正如罗素指出的,列表中的每个条目都有一个类别1项。我假设您想对所选项目执行某些操作。

这是由于。DataTemplate中的名称与外部的名称在不同的名称范围内,这就是为什么您不能访问它们(@Russell指出的是为什么它是这样做的一部分)

我认为您希望访问绑定到Details集合的HINList列表框的选定项上的“Category1”属性的字段。您可以将Category1上的绑定设置为双向,并将列表框的
SelectedItem
绑定到一个细节项,如下所示:

xaml:

希望这有帮助:)

这是由于。DataTemplate中的名称与外部的名称在不同的名称范围内,这就是为什么您不能访问它们(@Russell指出的是为什么它是这样做的一部分)

我认为您希望访问绑定到Details集合的HINList列表框的选定项上的“Category1”属性的字段。您可以将Category1上的绑定设置为双向,并将列表框的
SelectedItem
绑定到一个细节项,如下所示:

xaml:


希望这有帮助:)

每个列表项都会有一个文本块。如果您试图确定特定项目的类别1,为什么不在“详细信息”集合中使用实际项目本身?每个列表项目都会有一个文本块。如果您试图确定特定项目的类别1,为什么不在“详细信息”集合中使用实际项目本身?
if(SelectedDetailsItem.Category1==...)
{
   ....
}