locked
populate a listbox RRS feed

  • Question

  • Hi

    I have a listbox and I want to populate it (by code) with items,I want to allow the user to select one or more item so I want to place beside each item a checkbox so the user can select/diselect the item by clicking on the checkbox

    Here's my code

    listnoms.Items.Clear()

    For Each row As DataRow In dt.Rows

    sname = row("TABLE_NAME").ToString

    listnoms.Items.Add(sname)

    'Instead of simply adding sname I want to add a checkbox beside it

    Next

    Friday, May 2, 2008 1:11 PM

All replies

  • 1-Avoid adding items manually but use Databinding using ItemSource property
    2-Select the listbox the use Object->Edit Other Styles->Edit ItemContainerStyle then Edit the template replacing the contentpresenter with a CheckBox then use TemplateBinding to bing checkbox content and IsChecked property to ListBox's Item property.

    Style should result in:
            <Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">  
                <Setter Property="Background" Value="Transparent"/>  
                <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>  
                <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>  
                <Setter Property="Padding" Value="2,0,0,0"/>  
                <Setter Property="Template">  
                    <Setter.Value> 
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">  
                            <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">  
                                <CheckBox Content="{TemplateBinding Content}" IsChecked="{TemplateBinding IsSelected}"/>  
                            </Border> 
                            <ControlTemplate.Triggers> 
                                <Trigger Property="IsSelected" Value="true">  
                                    <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>  
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>  
                                </Trigger> 
                                <MultiTrigger> 
                                    <MultiTrigger.Conditions> 
                                        <Condition Property="IsSelected" Value="true"/>  
                                        <Condition Property="Selector.IsSelectionActive" Value="false"/>  
                                    </MultiTrigger.Conditions> 
                                    <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>  
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>  
                                </MultiTrigger> 
                                <Trigger Property="IsEnabled" Value="false">  
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>  
                                </Trigger> 
                            </ControlTemplate.Triggers> 
                        </ControlTemplate> 
                    </Setter.Value> 
                </Setter> 
            </Style> 
     


    Corrado Cavalli [MVP]
    Friday, May 2, 2008 7:56 PM
  • I had posted a sample on WPF forum. you can check it here - http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3288039&SiteID=1
    MVP Client App
    Monday, May 5, 2008 12:00 PM