我有一个listView。 ListView填充了一个数据集合(列表列表),但ListView中的每个项目都有相同的模板(边框,网格,边框,文本块),因此,当我选择一个模板时,如何获取文本块的text属性item ??
xmlns:vmSegments="using:EC_UsuarioWin8.ViewModels"
<Page.Resources>
<DataTemplate x:Key="ItemTemplateStyleSegment">
<Border Margin="-10,0,0,0" Background="White" >
<Grid Height="120" Width="200">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border Grid.Row="0">
<Image Source="{Binding SegmentImage}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10"/>
</Border>
<Border Grid.Row="1" >
<TextBlock Text="{Binding SegmentName}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#FFE0241B" FontSize="20" />
</Border>
</Grid>
</Border>
</DataTemplate>
</Page.Resources>
<Grid>
<Grid.Resources>
<vmSegments:ViewModelSegments x:Key="SegmentCollectionVM" x:Name="SegmentCollectionVM"/>
</Grid.Resources>
<ListView x:Name="ListVIewSegments" ItemsSource="{Binding Source={StaticResource SegmentCollectionVM}, Path= SegmentList}" Grid.Row="1" Margin="20" ItemContainerStyle="{StaticResource ListViewSItemtyleECFood}" ItemTemplate="{StaticResource ItemTemplateStyleSegment}" SelectionChanged="ListViewSegments_SelectionChanged">
</ListView>
</Grid>
我的类ViewModelSegments:
public class ViewModelSegments : BindableBase
{
private SegmentsCollection _segmentList = new SegmentsCollection();
public SegmentsCollection SegmentList
{
get { return _segmentList; }
set { SetProperty(ref _segmentList, value); }
}
}
我的班级SegmentsCollection:
public class SegmentsCollection: ObservableCollection<Segment>
{
}
我的课程栏目:
public class Segment : BindableBase
{
//Propiedad SegmentName
private string Name;
public string SegmentName
{
get { return Name; }
set { SetProperty(ref Name, value); }
}
//Propiedad SegmentPicture
private Uri image;
public Uri SegmentImage
{
get { return image; }
set { SetProperty(ref image, value); }
}
}
答案 0 :(得分:5)
使用ListView
ItemClick
事件
//Here sender is your list view
public delegate void ItemClickEventHandler(object sender, ItemClickEventArgs e)
{
ListView lv = sender as ListView;
Segment seg = e.ClickedItem as Segment;
if (lv != null)
{
//Use your lv if you want to
}
if (seg != null)
{
string name = seg. SegmentName;
}
}
<强>更新强>
private void ListView_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
lv.SelectedItem; // If you have set SelectionMode="Single" then it will do
//the trick but if you set SelectionMode="Multiple" then it's the first
//selected item
lv.SelectedItems; // It will give you the selected list of item view models
e.AddedItems; //This will give you the added items. As it is a list so can
//be multiple but when it can be multiple I don't know that case.
e.RemovedItems; //Similarly removed items
}
答案 1 :(得分:1)
在您的视图模型中创建如下属性:
public class ViewModelSegments : BindableBase, INotifyPropertyChanged
{
private SegmentsCollection _segmentList = new SegmentsCollection();
public SegmentsCollection SegmentList
{
get { return _segmentList; }
set { SetProperty(ref _segmentList, value); }
}
//Bind this property to your ListView SelectedItem property in the xaml
Segment _selectedSegment;
public Segment SelectedSegment
{
get
{
return _selectedSegment;
}
set
{
_selectedSegment=value;
NotifyPropertyChanged("SelectedSegment");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
在您的xaml中将ListView更改为:
<ListView x:Name="ListVIewSegments" ItemsSource="{Binding Source={StaticResource SegmentCollectionVM}, Path= SegmentList}" Grid.Row="1" Margin="20" ItemContainerStyle="{StaticResource ListViewSItemtyleECFood}" ItemTemplate="{StaticResource ItemTemplateStyleSegment}" SelectedItem="{Binding SelectedSegment}" SelectionChanged="ListViewSegments_SelectionChanged">
</ListView>
现在,只要ListView选项发生更改,SelectedSegment就会随之更改,然后您就可以访问SelectedSegment.SegmentName
答案 2 :(得分:0)
这样的东西?
string items = "";
foreach(ListViewItem item in listView1.SelectedItems){
items += "Item: " + item.Text + "\n";
foreach (ListViewItem.ListViewSubItem subitem in item.SubItems)
{
items += "SubItem: " + subitem.Text + "\n";
}
}
MessageBox.Show(items);
答案 3 :(得分:0)
Segment seg= ListView.SelectedItem as Segment;
if (seg!= null)
{
tbckChoiceSelected.Text = compa.CompanyName;//TextBlock called bckChoiceSelected
}
这很容易。再次,thnks。