C# 如何在XAML中访问列表框中项目的属性?
我有一个“SmartText”项目的列表框,这些项目基本上是带有标题、说明和url属性的链接对象。我试图使XAML中的网格面板可以点击,这样点击整个网格就可以导航到url。如何访问URL属性以便导航到它C# 如何在XAML中访问列表框中项目的属性?,c#,windows-phone-8,windows-phone,C#,Windows Phone 8,Windows Phone,我有一个“SmartText”项目的列表框,这些项目基本上是带有标题、说明和url属性的链接对象。我试图使XAML中的网格面板可以点击,这样点击整个网格就可以导航到url。如何访问URL属性以便导航到它 <controls:Pivot VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0,0,0,0" x:Name
<controls:Pivot VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Margin="0,0,0,0"
x:Name="PivotRoot"
Title="{Binding SmartTextStateModel.Title}"
SelectionChanged="Pivot_SelectionChanged"
Background="{StaticResource PhoneBackgroundBrush}">
<controls:PivotItem Header="{Binding Path=Labels.SmartTextBingHeaderLabel, Source={StaticResource Translations}}" Tag="bingsearch">
<ListBox ItemsSource="{Binding SmartTextStateModel.BingItemResults}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Tap="SmartTextElement_Tap">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" FontSize="40" Text="{Binding Path=Title}" />
<TextBlock Grid.Row="1" TextWrapping="Wrap" FontSize="18.667" Foreground="{StaticResource PhoneDisabledBrush}"
TextTrimming="WordEllipsis" MaxHeight="100" Text="{Binding Path=Description}"/>
<TextBlock Grid.Row="2" Foreground="{StaticResource PhoneAccentBrush}" Text="{Binding Path=Url}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PivotItem>
...
</controls:Pivot>
注意:这是最接近我的StackOverflow问题:,但它仍然没有帮助。网格位于
列表框的项目模板
内。这意味着Grid.DataContext
属性将成为SmartTextItemModel
类的实例:
private void SmartTextElement_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
var grid = sender as Grid;
if (grid == null)
return;
var item = grid.DataContext as SmartTextItemModel;
if (item == null)
return;
item.// Navigate to url...
}
我不明白你的问题。您在ViewModel中具有URL属性。事件处理程序中的发送方是一个网格面板元素。我尝试了多种方法从对网格面板的引用获取元素的url,但都没有效果。列表框中的每个项目都是不同的SmartText链接。点击
是否也选择列表框中的项目,您可以绑定到SelectedItem并在您的Handler中使用它谢谢,我不知道DataContext属性。我想我需要访问TextBlock子元素,然后以某种方式获取Url。
private void SmartTextElement_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
... ?
// Navigate to url...
}
private void SmartTextElement_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
var grid = sender as Grid;
if (grid == null)
return;
var item = grid.DataContext as SmartTextItemModel;
if (item == null)
return;
item.// Navigate to url...
}