2011-05-01 101 views
1

我有一個文本塊在列表框中,我試圖編寫一個依賴於此文本塊內容的if語句。我試圖從TextBlack中獲取數據,我已命名爲「category1」,但是當我嘗試寫入if語句時,我收到的消息只是說訪問列表框中包含的TextBlock

「名稱category1在當前上下文中不存在」

我累了將TextBlock移出列表框,它工作正常,但不會工作,而其內部。有誰知道如何引用這個文本塊。

這裏是我的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> 
+0

每個列表項都會有一個文本塊。如果您正在試圖確定Category1是否適用於特定項目,爲何不在Details集合中使用實際項目本身? – 2011-05-01 19:08:59

回答

1

假設你寫你如果代碼隱藏文件聲明,就不會是這樣的:

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

正如羅素指出的有列表中每個條目的category1項目。我假設你想對選定的項目做些什麼。

0

這是由於xaml namescopes。 DataTemplate中的名稱與外部名稱不同,這就是爲什麼你不能訪問它們(@Russell指出的是爲什麼這樣做的原因之一)。

我認爲你希望訪問綁定到Details集合的HINList ListBox的選定項上的「Category1」屬性的那個字段。你可以做的是設置在組別的結合是雙向的,並綁定列表框的SelectedItem到詳細項目,像這樣:

XAML:

<ListBox x:Name="HINList" ItemsSource="{Binding Details}" 
     SelectedItem={Binding SelectedDetailItem, Mode=TwoWay}> 
    <ListBox.ItemTemplate> 
    <DataTemplate> 
     <StackPanel Margin="0,0,0,17" Width="432"> 
     <TextBlock Text="{Binding Category1, Mode=TwoWay}" TextWrapping="Wrap" .../> 
     <!-- the other fields --> 
     </StackPanel> 
    </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

代碼隱藏

if(SelectedDetailsItem.Category1==...) 
{ 
    .... 
} 

希望這有助於:)