我想爲ListView內的複選框進行雙向綁定。這是我的產品類別:CheckBox雙向綁定不起作用
public class Product
{
public bool IsSelected { get; set; }
public string Name { get; set; }
}
在ViewModel類我的產品觀察到的集合:
private ObservableCollection<Product> _productList;
public ObservableCollection<Product> ProductList
{
get
{
return _productList;
}
set
{
_productList = value;
}
}
public MainViewModel()
{
ProductList = new ObservableCollection<Product>
{
new Product {IsSelected = false, Name = "Not selected"},
new Product {IsSelected = true, Name = "Selected"},
new Product {IsSelected = true, Name = "Selected"}
};
}
}
最後,我有網格,ListView控件是結合我的產品列表:
<Grid>
<ListView Height="120" HorizontalAlignment="Left"
VerticalAlignment="Top"
SelectionMode="Multiple"
ItemsSource="{Binding ProductList}" >
<ListView.View>
<GridView>
<GridViewColumn Width="40">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="120" Header="Product Name" DisplayMemberBinding="{Binding Path=Name}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
當我調試這個應用程序,當我檢查/取消選中複選框時,它永遠不會到達setter的行。 任何想法這段代碼有什麼問題? 在此先感謝!
http://stackoverflow.com/a/504936/440030 – 2011-12-27 08:49:15