2012-01-22 10 views
-1

私はWPFアプリケーションでListViewを持っています。私のListViewItemは単なる画像です。しかし、私はこのListViewの選択された値を取得したい。 ASP.Netでは、私はlistitemのText/Valueペアを設定することができ、selectedvalueは私が設定した値でした。ListView SelectedValue in WPF

どのようにwpfでこれを達成できますか?ここで

は私のXAMLです:

<ListView Name="lstStyle" MouseDoubleClick="lstStyle_MouseDoubleClick" KeyDown="lstStyle_KeyDown"> 
     <ListViewItem> 
      <Image Source="/WPFSample;component/Images/Home1.png"></Image> 

     </ListViewItem> 

任意のアイデア?

答えて

1

リストビューのItemsSourceを使用して、MyItemは、単にある

public ObservableCollection<MyItem> MyCollection; 
public int SelectedId; 

を持っているでしょう、あなたのListViewのDataContextので

<ListView ItemsSource="{Binding MyCollection}" 
      SelectedValuePath="Id" 
      SelectedValue="{Binding SelectedId}"> 

    <ListView.ItemTemplate> 
     <DataTemplate> 
      <Image Source="{Binding ImagePath}" /> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

自分のイメージパスを含むオブジェクトのコレクションと、あなたのIdフィールドにバインドこのようなクラス:

public class MyItem 
{ 
    public int Id { get; set; } 
    public string ImagePath { get; set; } 
} 

または代替として、あなたは良いデザインに興味を持っていないのであれば、ちょうどあなたのリストビュー項目はDataContextの中のコレクションとの結合にする必要がありますListViewItem

<ListViewItem Tag="1"> 
0

Tagプロパティを使用します。これを行うには、ItemsSourceプロパティを使用できます。 SelectedItemと呼ばれるAnoherプロパティを使用して、ListViewの選択した項目をDataContext内の別のプロパティにバインドできます。