1

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

<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",但它仍然没有帮助。

4

1 回答 1

2

Grid里面ItemTemplateListBox。这意味着该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...
}
于 2013-03-19T21:10:00.330 回答