Rayet
Rayet

Reputation: 298

Change item checkbox on ListBox selection change

I have a ListBox created by ItemTemplate and Binding

        <controls:PanoramaItem Header="{Binding AppResources.SettingsSubpage2, Source={StaticResource LocalizedStrings}}" HeaderTemplate="{StaticResource HeaderTemplate}">
            <Grid>
                <ListBox x:Name="DayOfWeekSelector" ItemTemplate="{StaticResource DayOfWeekTemplate}" ItemsSource="{Binding DayOfWeekElementList}" Foreground="{StaticResource AppForegroundColor}" LostFocus="DayOfWeekSelector_LostFocus" HorizontalAlignment="Left" Width="420" />
            </Grid>
        </controls:PanoramaItem>

Template code:

<phone:PhoneApplicationPage.Resources>
   <!--- ... --->
<DataTemplate x:Key="DayOfWeekTemplate">
        <Grid Height="65" Width="332">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <CheckBox IsChecked="{Binding IsActive, Mode=TwoWay}" Tag="{Binding}" d:LayoutOverrides="Width, Height" BorderBrush="{StaticResource AppBackgroundColor}" Background="{StaticResource ScheduleBackgroundAccentsColor}" Grid.Column="0" Unchecked="CheckBox_Unchecked" />
            <StackPanel Grid.Column="1" Orientation="Horizontal">
            <TextBlock Text="{Binding Name}" VerticalAlignment="Center"  d:LayoutOverrides="Width"/>
                <TextBlock Text="{Binding TaskCounter, Mode=OneWay, Converter={StaticResource DayOfWeekCounter}}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0"/>
            </StackPanel>
        </Grid>
    </DataTemplate>
   <!--- ... --->

And it's working fine. I've got all my items on the list. Checkboxes are binded to appropriate elements (clicking on it is changing proper value).

But by default ListBox can be also selected. Selection high-light TextBox binded to Name but don't change CheckBox (binded to IsActive). How can I tie item selection changing to checkbox state changing (in Silverlight)?

Edit:

public partial class SettingsPage : PhoneApplicationPage, INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    public List<DayOfWeekElement> DayOfWeekList
    {
        get
        {
            return CyberSyncPlanBase.Instance.DayOfWeekElementList;
        }
        set
        {
            CyberSyncPlanBase.Instance.DayOfWeekElementList = value;
            NotifyPropertyChanged("DayOfWeekList");
        }
    }

   public SettingsPage()
    {
        InitializeComponent();
        DayOfWeekSelector.DataContext = CyberSyncPlanBase.Instance;

    }

 private void DayOfWeekSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        DayOfWeekElement dowe = (DayOfWeekElement) DayOfWeekSelector.SelectedItem;

        if (dowe != null)
            dowe.IsActive = (dowe.IsActive) ? false : true;
    }

And in singleton INotifyPropertyChanged i've implemented in the same way:

    private List<DayOfWeekElement> dayOfWeekElementList;
    public List<DayOfWeekElement> DayOfWeekElementList 
    { 
        get { return dayOfWeekElementList; }
        set 
        { 
        dayOfWeekElementList = value;
        RecalcWeekTasks();
        NotifyPropertyChanged("DayOfWeekElementList");
        } 
    }

Bottom class:

public class DayOfWeekElement
{
    public string Name { get { return this.DayOfWeek.ToStringValue(); } }
    public bool IsNotEmpty { get { return (TaskCounter > 0); } }
    public int TaskCounter { get; set; }
    public bool IsActive { get; set; }
    public DayOfWeek DayOfWeek { get; set; }
}

Upvotes: 0

Views: 816

Answers (1)

Alberto Solano
Alberto Solano

Reputation: 8227

I think you could use the SelectedItem property of the ListBox control.

A possible implementation could be this:

  1. Subscribe to the event SelectedIndexChanged of the ListBox.
  2. Get the selected item.
  3. For the selected item, change its IsActive property to true.

This works if the interface INotifyPropertyChanged is implemented in your data class.

E.g.:

public class DayOfWeekElement : INotifyPropertyChanged
{
   public event PropertyChangedEventHandler PropertyChanged;

   private void NotifyPropertyChanged(string propertyName)
   {
       if (PropertyChanged != null)
       {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
       }
   }


   private bool isActive = false;
   public bool IsActive { 
      get
      {
          return this.isActive;
      } 
      set
      {
          if (value != this.isActive)
          {
             this.isActive= value;
             NotifyPropertyChanged("IsActive");
          }
      }  
   }
}

Upvotes: 1

Related Questions