0

わかりました、あなたが私を理解してくれることを願っています...

私はリストビューを持っています。ListView はデータのコレクション (リスト リスト) で満たされていますが、ListView 内の各項目には同じテンプレート (境界線、グリッド、境界線、テキストブロック) があります。アイテム ??

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

}
4

4 に答える 4

5

の使用ItemClickイベントListView

//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
}
于 2013-02-02T06:04:38.300 に答える
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

于 2013-02-01T23:22:27.973 に答える
0

Inder Kumar Rathore と DJ Burb に感謝し、答えを見つけてくれました。だから、ここにある

Segment seg= ListView.SelectedItem as Segment;
if (seg!= null)
{
    tbckChoiceSelected.Text = compa.CompanyName;//TextBlock called bckChoiceSelected
}

とても簡単でした。もう一度、ありがとう。

于 2013-02-14T14:55:24.453 に答える
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);
于 2013-02-01T23:05:05.467 に答える