2011-12-19 108 views
0

我有喜歡若要選擇列表視圖中的複選框值在WPF

<ListView ItemsSource="{Binding}" Height="110.277" Margin="4,0,3,-138" Name="listView1" VerticalAlignment="Bottom"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 

     <!--<TextBlock Text="{Binding Path=Name}" Width="100" />--> 
     <!--<CheckBox IsChecked="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}" Width="100"/>--> 
     <CheckBox Name="chk1" Content="{Binding Path=Name}" IsChecked="{Binding IsPersonChecked}" Checked="checked_accname" Width="100" /> 

     </DataTemplate> 
</ListBox.ItemTemplate> 

的代碼,我會爲複選框值動態地從數據庫在ButtonClick事件我能不能綁定獲取listview的checked複選框的值。

請幫我解決這個問題。在此先感謝

+0

是什麼問題?你的代碼應該可以工作 – 2011-12-19 09:21:05

回答

1

不要試圖從UI獲取選中的值。使用數據對象的IsPersonChecked屬性。

var persons = listView1.DataContext as Persons; 
var selectedPersonsQuery = from person in persons 
          where person.IsPersonChecked 
          select person; 

編輯

瞭解你使用的數據視圖的查詢會是這樣的後:

var dataView = listView1.DataContext as DataView; 
var selectedPersonRowsQuery = from row in dataView 
           where row["IsPersonChecked"] 
           select row; 
+0

你好,你已經提到listview1.DataContext中的人員爲人員(什麼是人員),我正在查看人員錯誤 – sidd2004k 2011-12-21 03:26:15

+0

@ karthik2004k - 人員是綁定到列表框的對象的集合。 – 2011-12-21 05:19:09

+0

您好我已經在windowload事件中動態綁定了複選框的值,如DataSet dss = dal.get_user(); listView1.ItemsSource = dss.Tables [0] .DefaultView;所以我怎麼能在運行時在這種情況下檢查複選框的值 – sidd2004k 2011-12-21 06:23:25