user2837961
user2837961

Reputation: 1555

Refer to value from ItemControl

I have a itemscontrol as below

   <StackPanel>
        <ItemsControl Name="PlannerItemControl" Grid.Row="0" Grid.Column="0">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid Name="MainGrid" Style="{StaticResource VisibleKey}">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="35" />
                        </Grid.RowDefinitions>
                        <Label Grid.Row="1" Grid.Column="2" Name="lblTimeText" Content="{Binding ID}" HorizontalAlignment="Center"/>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
    <Label Grid.Row="1" Grid.Column="2" Name="lblTime2" Content="{Binding ID}" HorizontalAlignment="Center"/>

Is it possible to retrieve the value of ID from the itemscontrol outside of the itemscontrol? Basically I want the value of Label lblTimeText in label lblTime2. Please help.

Upvotes: 0

Views: 60

Answers (1)

VisualBean
VisualBean

Reputation: 5008

The ItemsControl does not track SelectedItem - instead use a ListBox. (The selectedItem should be the same type as your collection items).

To track a selectedItem you need to bind the selectedItem property of the listbox to a property in your ViewModel

<Listbox ItemsSource="{Binding Path=YourCollection}" SelectedItem="{Binding YourItem}">

(remember INotifyChanged)

and then bind that item.id to your label

 <Label Grid.Row="1" Grid.Column="2" Name="lblTime2" Content="{Binding YourItem.ID}" HorizontalAlignment="Center"/>

Upvotes: 1

Related Questions