0

I have a combobox which data source comes from a table in my DataBase. So, each item in my combo is an Object from the table. This Object have an attribute which corresponds to a string full of "1"s or "0"s. On the other hand I have a list of checkboxes inside of a ListBox with this template:

   <ListBox Height="150" MinHeight="100" HorizontalAlignment="Center" Name="lstEstudios" VerticalAlignment="Top" Width="200" 
                                 ItemsSource="{Binding}" SelectionMode="Multiple" Margin="0,20,0,0">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <CheckBox Name="chkEstudios" Width="Auto"  Content="{Binding Path=Nom_estudio}" 
                                          Checked="chkEstudios_Checked" Unchecked="chkEstudios_Unchecked"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

I don´t know if it's possible but, that I want to do is, for each "1" or "0" in the attribute set the checkbox checked or unchecked depending if there is a "1" check the checkbox or if is "0" uncheck the checkbox, and so on... with all the checkboxes in the ListBox, how to do that ?

2 Answers2

3

I tried the same thing with my own sample having a CustomTask class.

       <ListBox ItemsSource="{Binding CustomTasks}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding TaskStatus, Converter={x:Static testApp:StatusToBooleanConverter.Instance}}" Content="{Binding TaskStatus}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

where the TaskStatus is a boolean of two values, i.e Completed and Pending.

and here is the code for the converter

 public class StatusToBooleanConverter : IValueConverter
{
    public static StatusToBooleanConverter Instance = new StatusToBooleanConverter();
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is Status)
        {
            switch ((Status)value)
            {
                case Status.Completed:
                    return true;
                case Status.Pending: 
                    return false;
            }
            return null;
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Just try this out. Hope that helps.

Mayur Dhingra
  • 1,527
  • 10
  • 27
0

This is a good place to use a converter. If you have a property with 1's and 0's in it and you want to translate those to true/false for the checked attribute then try making converters to do the work. The basic idea behind a converter is to take an input value (from a bound property) and, as the name implies, convert it to a different value. You can make them as simple or complicated as you would like, and turning "1" into true and "0" into false should be pretty quick.

You will bind the IsChecked attribute of the checkbox to your source of 1's and 0's and in the binding also use the converter.

If you haven't made a value converter before here is a nice tutorial on making one: http://wpftutorial.net/ValueConverters.html

zaknotzach
  • 977
  • 1
  • 9
  • 18