S.John Hawkins

Hi friends,

How to change a Listbox into a Checked listbox in XAML..Can anyone help me with code for binding the data(in checked listbox) from the backend. I am using C# coding.



Re: Windows Presentation Foundation (WPF) How to change a Listbox into a Checked listbox in XAML..

WPCoder

A checkbox in a listbox is simple:

<Page xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Page.Resources>
<XmlDataProvider x:Key="Employees" XPath="/Employees/*">
<x:XData>
<Employees xmlns="">
<Employee Name="Bonnie Harken"
InOffice="True"/>
<Employee Name="Stephan Norsiski"
InOffice="True"/>
<Employee Name="Kyle Franks"
InOffice="False"/>
<Employee Name="Aaron Upahana" InOffice="True" />
<Employee Name="Mike Hanks" InOffice="True"/>
</Employees>
</x:XData>
</XmlDataProvider>
<DataTemplate x:Key="EmployeeItemTemplate">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding XPath=@InOffice}" />
<TextBlock Text="{Binding XPath=@Name}" />
</StackPanel>
</DataTemplate>
</Page.Resources>
<StackPanel>
<ListBox x:Name="listEmployees" TextSearch.TextPath="@Name"
ItemTemplate="{StaticResource EmployeeItemTemplate}"
ItemsSource="{Binding Source={StaticResource Employees}}" >
</ListBox>
</StackPanel>
</Page>

Then, the only difference in code would be that you would bind to a collection of Employees and set the data context properly in code (or directly):


listEmployees.ItemSource = myEmployeeList;

public class Employee {
properties ... Name, InOffice, etc..
}
public class Employees : ObservableCollection<Employee>
{ }

Remove the ItemsSource binding in the XAML if you bind directly in code.
The property bindings change from using XPath syntax to this:

"{Binding Path=Name}"






Re: Windows Presentation Foundation (WPF) How to change a Listbox into a Checked listbox in XAML..

Gary Gumbiner

Great listbox example. Quick question, what purpose does the listbox XAML TextSearch.TextPath="@Name" serve

Also, if you want to get selected item information the SelectionChanged event with VB code will do the trick:

Private Sub listEmployees_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles listEmployees.SelectionChanged

System.Diagnostics.Debug.WriteLine("Name: " & e.Source.selecteditem.attributes(0).value)

System.Diagnostics.Debug.WriteLine("InOffice: " & e.Source.selecteditem.attributes(1).value)

End Sub






Re: Windows Presentation Foundation (WPF) How to change a Listbox into a Checked listbox in XAML..

S.John Hawkins

Thanks Gary and wpfcoder..I got an idea abt it..





Re: Windows Presentation Foundation (WPF) How to change a Listbox into a Checked listbox in XAML..

WPCoder

TextSearch.Text="@Name":

It forces the ListBox to use the Name element as the text search when a user starts typing within the list box (usually to jump to a particular item quickly).