0
我有一個複選框列表視圖中的每一行。我想在視圖模型中將複選框OnClick綁定到我的命令。但它對我的Command沒有約束力。我該怎麼辦?下面是我的XAML和視圖模型綁定複選框命令
的XAML:
<Grid>
<ListView Name="lstdemo" ItemsSource="{Binding obcollection}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chk" IsChecked="{Binding IsChecked,Mode=TwoWay,NotifyOnTargetUpdated=True}" Command="{Binding Path=UpdateCommand}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Grid>
視圖模型:
public class MyViewModel
{
private List<Demo> lstdemo;
private ObservableCollection<Demo> Obcollection;
private SampleDb db;
public MyViewModel()
{
db = new SampleDb();
lstdemo = db.Demoes.ToList();
Convert();
}
public void Convert()
{
Obcollection = new ObservableCollection<Demo>(lstdemo);
}
public ObservableCollection<Demo> obcollection
{
get { return Obcollection; }
set { Obcollection = value; }
}
private ICommand mUpdater;
public ICommand UpdateCommand
{
get
{
if (mUpdater == null)
mUpdater = new Updater();
return mUpdater;
}
set
{
mUpdater = value;
}
}
public class Updater : ICommand
{
#region ICommand Members
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
if (parameter == null)
{
MessageBox.Show("Hello");
}
}
#endregion
}
}
真棒..感謝它的工作 – nikhil 2014-09-22 19:27:41
@nikhil歡迎您! – 2014-09-22 19:28:51