How can one access the properties of the Textblock(txtlink) in the below XAML code from c#? Referencing txtlink. doesn't work and it results in an error
The name txtlink doesn't exist in the current context
<Grid Grid.Row="1">
<ListBox ItemsSource="{Binding CurrentRss.Channel.NewsItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Style="{StaticResource ImageButtonStyle}" Click="Button_Click" >
<Image Source="{Binding Image}"/>
</Button>
<TextBlock Grid.Column="1" Text="{Binding Title}"/>
<TextBlock x:Name="txtlink" Text="{Binding Link}" Background="Black" Foreground="#FFD1DA0B"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
Use SelectedItem binding, then you will have access to the selected item. From there you can access the Link property.
xaml:
<ListBox ItemsSource="{Binding CurrentRss.Channel.NewsItems}" SelectedItem="{Binding SelectedNewsItem}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Style="{StaticResource ImageButtonStyle}" Click="Button_Click" >
<Image Source="{Binding Image}"/>
</Button>
<TextBlock Grid.Column="1" Text="{Binding Title}"/>
<TextBlock x:Name="txtlink" Text="{Binding Link}" Background="Black" Foreground="#FFD1DA0B"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and in your ViewModel:
public NewsItem SelectedNewsItem {get;set;}//INPC when it is being set check for null and then process the selected item.
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.