简体   繁体   English

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

[英]How do I access the property of an item in a ListBox in XAML?

I have a listbox of "SmartText" items, which are basically link objects with properties for a title, description, and a url. 我有一个“ SmartText”项的列表框,这些项基本上是具有标题,描述和url属性的链接对象。 I'm trying to make the Grid panel in the XAML tappable, so that tapping on the whole Grid navigates to the url. 我正在尝试使XAML中的“网格”面板可轻按,以便在整个Grid上轻按都会导航到该URL。 How do I do access the URL property so that I can navigate to it? 如何访问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>

and the class definition 和类的定义

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

Of course the event handler in the .cs file looks like this: 当然,.cs文件中的事件处理程序如下所示:

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

Note: This was the closest StackOverflow question to mine: How to get the listbox item's properties after event "tap" , but it still didn't help. 注意:这是我要研究的最接近的StackOverflow问题: 如何在事件“ tap”之后获取列表框项目的属性 ,但仍然无济于事。

the Grid is inside the ItemTemplate of the ListBox . Grid是里面ItemTemplate中的ListBox That means that the Grid.DataContext property is going to be an instance of your SmartTextItemModel class: 这意味着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...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM