tags:

views:

660

answers:

2

Near total WPF noob. So I hooked up a combobox to have checkboxes using the following item template:

<ComboBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <CheckBox Checked="{Binding IsSelected}"
                   Width="20" Name="chkDayName" Click="chkDayName_Click"/>
            <TextBlock Text="{Binding DayOfWeek}"
                   Width="100"  Name="txtDayName" />
        </StackPanel>
    </DataTemplate>
</ComboBox.ItemTemplate>

On the actual event of a person clicking a checkbox, i catch the event in chkDayName_Click method. I have the following questions:

How do I find out values of the corresponding TextBlock in the item template? How do i find out the index of the item that was clicked?
Is there a way to get to the parent?

Thanks.

+1  A: 

If I understand it you want to know which combobox items are checked? You can use the chkDayName_Click for that and add the name of the day as Tag of the CheckBox. This feels very Winforms. In WPF you normally let your databinding handle functionality like this. Below is some code that will display selected item in a textbox and a list of checked weekdays.

XAML:

<Window x:Class="DayComboBoxDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">

    <Window.Resources>

        <CollectionViewSource x:Key="checkedWeekdays" Source="{Binding Path=WeekDays}" Filter="IsCheckedFilter" />

    </Window.Resources>

    <StackPanel>
        <ComboBox
            ItemsSource="{Binding Path=WeekDays}"
            SelectedItem="{Binding Path=SelectedWeekDay}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox 
                            IsChecked="{Binding Path=IsChecked}"
                            Width="20" Click="chkDayName_Click"/>
                        <TextBlock 
                            Text="{Binding DayOfWeek}" Width="100" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
        <TextBlock Text="{Binding Path=SelectedWeekDay.DayOfWeek}" />
        <ListBox
            DisplayMemberPath="DayOfWeek"
            ItemsSource="{Binding Source={StaticResource checkedWeekdays}}" />
    </StackPanel>
</Window>

Code behind:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Data;

namespace DayComboBoxDemo
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            List<WeekDay> weekDays = new List<WeekDay>();
            foreach (DayOfWeek dayOfWeek in System.Enum.GetValues(typeof(DayOfWeek)))
            {
                weekDays.Add(new WeekDay() { DayOfWeek = dayOfWeek });
            }

            WeekDays = weekDays;

            _checkedWeekdays = FindResource("checkedWeekdays") as CollectionViewSource;

            DataContext = this;
        }

        public IEnumerable<WeekDay> WeekDays { get; set; }

        public WeekDay SelectedWeekDay
        {
            get { return (WeekDay)GetValue(SelectedWeekDayProperty); }
            set { SetValue(SelectedWeekDayProperty, value); }
        }

        public static readonly DependencyProperty SelectedWeekDayProperty =
            DependencyProperty.Register("SelectedWeekDay", 
                                        typeof(WeekDay), 
                                        typeof(Window1), 
                                        new UIPropertyMetadata(null));

        private void chkDayName_Click(object sender, RoutedEventArgs e)
        {
            _checkedWeekdays.View.Refresh();
        }

        private void IsCheckedFilter(object sender, FilterEventArgs e)
        {
            WeekDay weekDay = e.Item as WeekDay;
            e.Accepted = weekDay.IsChecked;
        }

        private CollectionViewSource _checkedWeekdays;
    }

    public class WeekDay
    {
        public DayOfWeek DayOfWeek { get; set; }
        public bool IsChecked { get; set; }
    }
}
Wallstreet Programmer
A: 

You can try ComboBox's SelectedIndex or SelectedValue to tell the SelectedItem. In the MVVM fashion, you can have a two-way binding between SelectedIndex and one of you ViewModel properties.

Kai Wang