2013-03-19 85 views
1

我有一个“SmartText”项目的列表框,它基本上是链接对象,具有标题,描述和url的属性。我正在尝试使XAML中的Grid面板可点击,以便在整个Grid上点击导航到该URL。我如何访问URL属性以便我可以导航到它?如何访问XAML中ListBox中的项目的属性?

<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> 

和类定义

public class SmartTextItemModel : BaseModel 
{ 
    private string _title; 
    private string _description; 
    private string _url; 

    /// <summary> 
    /// The title of the linked page (Large text) 
    /// </summary> 
    public string Title 
    { 
     get { return _title; } 
     set 
     { 
      if (_title != value) 
      { 
       _title = value; 
       NotifyPropertyChanged("Title"); 
      } 
     } 
    } 

    /// <summary> 
    /// Description of the page (smaller text) 
    /// </summary> 
    public string Description 
    { 
     get { return _description; } 
     set 
     { 
      if (_description != value) 
      { 
       _description = value; 
       NotifyPropertyChanged("Description"); 
      } 
     } 
    } 

    /// <summary> 
    /// Url of the page 
    /// </summary> 
    public string Url 
    { 
     get { return _url; } 
     set 
     { 
      if (_url != value) 
      { 
       _url = value; 
       NotifyPropertyChanged("Url"); 
      } 
     } 
    } 

    public SmartTextItemModel(string _t, string _d, string _u) 
    { 
     this._title = _t; 
     this._description = _d; 
     this._url = _u; 
    } 
} 

当然在cs文件的事件处理程序是这样的:

private void SmartTextElement_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
{ 
    ... ? 
    // Navigate to url... 
} 

注:这是最接近的StackOverflow问题我的: How to get the listbox item's properties after event "tap",但它仍然没有帮助。

+0

我不明白你的问题。您在ViewModel中具有URL属性。只要从那里得到它。 – 2013-03-19 21:00:26

+0

事件处理程序中的发件人是一个Grid面板元素。我已经尝试了多种方式从引用到Grid面板到达元素的url,但没有任何工作。列表框中的每个项目都是不同的SmartText链接。 – Flames 2013-03-19 21:07:26

+0

'Tap'是否也在ListBox中选择Item,你可以绑定到SelectedItem并在你的处理器中使用它 – 2013-03-19 21:19:00

回答

2

Grid位于ListBoxItemTemplate之内。这意味着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... 
} 
+0

谢谢,我没有意识到DataContext属性。我想我需要访问TextBlock子元素,然后以某种方式获取Url。 – Flames 2013-03-19 21:25:45

相关问题