简体   繁体   中英

Access controls inside listbox c# (windows Phone 8)

         my xaml  code..
      <phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="DataTemplate1" >
        <Border BorderBrush="LightGray" BorderThickness="1" Height="150"            Width="500" >
            <Grid Width="500" Height="150" Background="White" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.9*"/>
                    <ColumnDefinition Width="2.5*"/>
                    <ColumnDefinition Width="1.5*"/>

                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Image Name="imgitem"  Grid.Column="0" Height="Auto" Width="Auto" Source="{Binding ImgSource}" Margin="0,5,4,4" HorizontalAlignment="Left" />
                <TextBlock x:Name="txtbindprice"  Text="{Binding _PRICE,ConverterCulture=en-IN,StringFormat=C}" TextWrapping="Wrap"  Grid.Column="1"  Width="350"  Foreground="Black" Height="60" Margin="40,70,20,-10"/>
                <TextBlock x:Name="txtFinalTotal"  Text="{Binding _FinalTotal}" TextWrapping="Wrap"  Grid.Column="1"  Width="350"  Foreground="Red" Height="60" Margin="40,115,20,-10"/>
                <TextBlock x:Name="txtITMNAME"  Text="{Binding _ITMNAME }" Padding="1"  Tap="ItmName_Tapped" TextDecorations="Underline" FontSize="24" TextWrapping="Wrap" Grid.Column="1"  FontWeight="Normal" TextTrimming="WordEllipsis"  Foreground="OrangeRed" Width="Auto"  Height="150"  Margin="30,25,10,-10"/>
                <CheckBox  Grid.Column="2" Grid.Row="0" Background="Black" Foreground="Black" BorderThickness="4" BorderBrush="Red" Margin="10,20,-15,10"  Checked="CheckBox_Checked" Unchecked="CheckBox_UnChecked"  /> 

            </Grid>
        </Border>
    </DataTemplate>

     <ListBox Height="Auto" Name="lstbxmanual" SelectionMode="Extended" ItemTemplate="{StaticResource DataTemplate1 }"  Width="475" Margin="2,192,0,-39" Background="White"  HorizontalAlignment="Left" Grid.RowSpan="2">
    </ListBox>

I want to access textblocks inside listbox based on selected index

accessing textblock 
  string  a =txtbindprice.text

wont work as they are inside data template of listbox..

I came across visual child tree methods and some other examples..i cant find much info..

please help me regarding this...

The following method finds currently selected list-box item:

    private ListBoxItem FindSelectedListBoxItem(DependencyObject control)
    {
        int childNumber = VisualTreeHelper.GetChildrenCount(control);
        for (int i = 0; i < childNumber; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(control, i);
            FrameworkElement fe = child as FrameworkElement;
            // Not a framework element or is null
            if (fe == null) return null;

            if (child is ListBoxItem && ((ListBoxItem)child).IsSelected)
            {
                // Found the control so return
                return (ListBoxItem)child;
            }

            else
            {
                // Not found it - search children
                ListBoxItem nextLevel = FindSelectedListBoxItem(child);
                if (nextLevel != null)
                    return nextLevel;
            }
        }
        return null;
    }

Below method finds any child control with a certain name from a given root control:

private DependencyObject FindChildControl<T>(DependencyObject control, string ctrlName)
{
    int childNumber = VisualTreeHelper.GetChildrenCount(control);
    for (int i = 0; i < childNumber; i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(control, i);
        FrameworkElement fe = child as FrameworkElement;
        // Not a framework element or is null
        if (fe == null) return null;

        if (child is T && fe.Name == ctrlName)
        {
            // Found the control so return
            return child;
        }
        else
        {
            // Not found it - search children
            DependencyObject nextLevel = FindChildControl<T>(child, ctrlName);
            if (nextLevel != null)
                return nextLevel;
        }
    }
    return null;
}

Now you can use the first method to get currently selected ListBoxItem and then pass it to the second method to retrieve any control inside this ListBoxItem (eg txtbindprice ):

ListBoxItem item = FindSelectedListBoxItem(this);
TextBlock txtPrice = FindChildControl<TextBlock>(item , "txtbindprice") as TextBlock;
string a = txtPrice.Text;

Update

You can add SelectionChanged event to your ListBox like this:

 <ListBox Height="Auto" Name="lstbxmanual" SelectionChanged="OnSelectionChanged">
</ListBox>

And then retrieve a certain TextBlock.Text (eg txtbindprice ) this way:

public void OnSelectionChanged(object sender, SelectionChangedEventArgs args)
{
    ListBoxItem item = FindSelectedListBoxItem((ListBox(sender)));
    TextBlock txtPrice = FindChildControl<TextBlock>(item , "txtbindprice") as TextBlock;
    string a = txtPrice.Text;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM