Reputation: 3442
I am trying to get Checked numbers from checkbox.
<Window x:Class="WpfApplication6.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local ="clr-namespace:WpfApplication6"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:Test x:Key="TestContext"></local:Test>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding TestList}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding number}" Command="{Binding WhatChecked,Source={StaticResource TestContext},Mode=OneWay}" IsChecked="{Binding check}" CommandParameter="{Binding check}"></CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
But i can pass only one parameter
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
namespace WpfApplication6
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public class DataStruct
{
public string number {get;set;}
public bool check {get;set;}
}
public class Test
{
public Test()
{
WhatChecked = new RelayCommand<bool>((e) => Show(e));
DataStruct DS = new DataStruct();
DS.check = false;
DS.number = "101";
DataStruct DS1 = new DataStruct();
DS1.check = false;
DS1.number = "102";
DataStruct DS2 = new DataStruct();
DS2.check = false;
DS2.number = "103";
testList = new List<DataStruct>();
TestList.Add(DS);
TestList.Add(DS1);
TestList.Add(DS2);
}
public ICommand WhatChecked { get; set; }
private List<DataStruct> testList;
public List<DataStruct> TestList
{
get { return testList; }
set { testList = value; }
}
private void Show(object param)
{
if (Convert.ToBoolean(param))
{
Debug.WriteLine("Clicked: {0}", param);
}
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new Test();
}
}
}
But i can not pass string with number into command parameter because i already have parameter.
Upvotes: 0
Views: 1849
Reputation: 4116
Since all the items are in the list, and all items are bound to the checkbox IsChecked
property, you can just search in your list using LINQ
TestList.Where(p=>p.check).Count();
Upvotes: 1
Reputation: 1553
If you set the CommandParameter binding as {Binding} then it'll pass the CheckBox's DataContext (i.e. the entire DataStruct object) as the parameter. This should allow you to access all of the DataStruct properties, rather than just check. Note that many people consider this a violation of MVVM principles in that the ViewModel becomes somewhat more tightly tied to/dependent upon the View. I've used this method a few times where I felt it was necessary to get the job done.
<CheckBox Content="{Binding number}" Command="{Binding WhatChecked,Source={StaticResource TestContext},Mode=OneWay}" IsChecked="{Binding check}" CommandParameter="{Binding}"></CheckBox>
Upvotes: 0