views:

32

answers:

1

i want to get value of listbox datatemplate item checkbox get value(check/uncheck) on button click.

A: 

The easiest way to accomplish this is to use an object that represents the checked state that the ListBox is binding to. Here's an example using a ViewModel pattern. The ListBox is binding to the Options collection, which is a collection of Option objects. The Option object has a "IsSelected" property which is two-way bound to the CheckBox in the DataTemplate.

The Option Class:

public class Option : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            RaisePropertyChanged("Name");
        }
    }

    private bool _isSelected;
    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            _isSelected = value;
            RaisePropertyChanged("IsSelected");
        }
    }

    protected void RaisePropertyChanged(string propertyName)
    {
        var handler = this.PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

The MainPageViewModel:

public class MainPageViewModel
{
    public MainPageViewModel()
    {
        this.Options = new ObservableCollection<Option>();
        for (int i = 0; i < 5; i++)
        {
            this.Options.Add(
                new Option
                {
                    Name = "Option " + (i + 1).ToString(),
                    IsSelected = i % 2 == 0
                });
        }
    }

    public IEnumerable<Option> SelectedOptions
    {
        get
        {
            return this.Options.Where(x => x.IsSelected);

        }
    }

    private ObservableCollection<Option> _options;
    public ObservableCollection<Option> Options
    {
        get { return _options; }
        set
        {
            _options = value;
        }
    }
}

MainPage.xaml:

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Button
        Content="Get Selected Options"
        Click="SelectedOptions_Click"/>
    <ListBox
        ItemsSource="{Binding Options}"
        Margin="30"
        Grid.Row="1">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <CheckBox
                        IsChecked="{Binding IsSelected, Mode=TwoWay}"
                        Content="{Binding Name}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

MainPage.xaml.cs:

public partial class MainPage : UserControl
{
    private MainPageViewModel _viewModel;

    public MainPage()
    {
        InitializeComponent();

        _viewModel = new MainPageViewModel();

        this.DataContext = _viewModel;
    }

    private void SelectedOptions_Click(object sender, RoutedEventArgs e)
    {
        int count = this._viewModel.SelectedOptions.Count();

        MessageBox.Show(string.Format("{0} options selected", count));
    }
}

Hope that helps.

Joe McBride
Thanxs.your ans is only binding time get value. i need binding data with listbox dynamic then after i want check/uncheck some checkbox.that value i want.i solve this issue use of check and uncheck checkbox event.
Pratik
Thanxs.But your ans is only binding time get value. i need binding data with listbox dynamic then after i want check/uncheck some checkbox.that value i want.i solve this issue use of check and uncheck checkbox event.
Pratik
I apologize though I don't fully understand what you're saying. My example is showing that you *should not* use the Checked/UnChecked events and instead use a Model and bind to that Model.
Joe McBride