如何在XAML中访问ListBox中项目的属性?

时间:2013-03-19 20:58:31

标签: c# windows-phone-8 windows-phone

我有一个“SmartText”项目的列表框,它们基本上是具有标题,描述和网址属性的链接对象。我正在尝试使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",但它仍然无效。

1 个答案:

答案 0 :(得分:2)

Grid位于ItemTemplate的{​​{1}}内。这意味着ListBox属性将成为Grid.DataContext类的实例:

SmartTextItemModel