开发者

Accessing a TextBlock which is contained inside a ListBox

I have a textblock which is inside a listbox and I am trying to write an if statement which is dependant on the contents of this textblock. I am trying to get the data from the TextBlack which I have named "category1" however when I try to write my if statement I am getting a message which just says

"the name category1 does not exist in the current context"

I tired moving that TextBLock out of the ListBox and it works fine but wont work while its inside there. Does anyone know how to reference this textblock.

Here is the my XAML code

        <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" Marg开发者_如何学编程in="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>


Assuming you're writing your if statement in the code behind file, wouldn't something like:

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

As Russell pointed out there is a category1 item for every entry in the list. I assume you wanted to do something with the selected item.


This is due to xaml namescopes. The names inside a DataTemplate are in a different namescope than outside, that's why you can't access them (what @Russell pointed is part of why it's done this way).

I think that you want to access that field for the "Category1" property on the selected item of the HINList ListBox that is bound to the Details collection. What you can do is set the binding on the Category1 to be two way, and bind the SelectedItem of the ListBox to a Detail item like so:

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>

code-behind

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

Hope this helps :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜