Question

When I select a listbox item I would like to have that data passed to another textblock.

Way I have it setup: Inside my listbox.itemtemplate I have two textblocks inside a stackpanel. The text inside the listbox is bounded from a class .

Problem: Since the textblock is inside a listbox.item, I am unable to call the textblock in the mainpage.cs, while I can call the data in a textblock that is in a grid.

How can I get the text from the textblock from inside the listbox item to pass to another textblock or label.

Était-ce utile?

La solution

The way I do it usually is I do a TwoWay binding of the SelectedItem property on the list control with a similarly named property on the view model and once I have that - my selected item view can bind to the view model. Of course you can bind directly from the selected item view to the list control's SelectedItem and further on to the properties of the item properties using ElementName binding, but that misses the point of separation of concerns.

Example for the dirty approach:

<ListView
    x:Name="lv">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Property1}" />
                <TextBlock Text="{Binding Property2}" />
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView

<Grid
    x:Name="SelectedItemView">
    <TextBlock Text="{Binding SelectedItem.Property1, ElementName='lv'}" />
</Grid>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top