![](/img/trans.png)
[英]How do I apply the textbox SelectionBrush property to a listbox item?
[英]How do I access the property of an item in a ListBox in XAML?
我有一個“ SmartText”項的列表框,這些項基本上是具有標題,描述和url屬性的鏈接對象。 我正在嘗試使XAML中的“網格”面板可輕按,以便在整個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問題: 如何在事件“ tap”之后獲取列表框項目的屬性 ,但仍然無濟於事。
該Grid
是里面ItemTemplate
中的ListBox
。 這意味着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.