binding observable list to listbox with templete listbox items

Go To


I have the following templete

<ControlTemplate x:Key="tmpl_btn_TecnicianModeMenu" TargetType="{x:Type ListBoxItem}">
<Grid Opacity="1" d:LayoutOverrides="Width, Height">
        BorderThickness="0" Height="Auto" Margin="0" Background="White">

        <StackPanel Name="stackPanel" Height="Auto" Margin="0" Orientation="Horizontal" >
            <Button x:Name="button" Style="{DynamicResource ButtonListBoxItem}" Margin="5,5,5,5" Width="120" Height="Auto" BorderThickness="0" >
                <TextBlock x:Name="textBlock" TextWrapping="Wrap" Text="בצע"  Margin="12,0" VerticalAlignment="Center" HorizontalAlignment="Stretch" Style="{DynamicResource tb_Desc}"/>
            <StackPanel Height="Auto" Margin="0" Orientation="Horizontal" >
                <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" x:Name ="LB_Result"  Text="LB_Result" Style="{DynamicResource LB_AreaTitle_Balance}" Margin="5,5,5,5" d:LayoutOverrides="Height" />
                <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" x:Name ="LB_OK"  Text="LB_OK" Style="{DynamicResource LB_AreaTitle_Balance}" Margin="5,5,5,5" d:LayoutOverrides="Height" />
                <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" x:Name ="LB_TchName"  Text="LB_TchName" Style="{DynamicResource LB_AreaTitle_Balance}" Margin="5,5,5,5"/>
                <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" x:Name ="LB_Date"  Text="LB_Date" Style="{DynamicResource LB_AreaTitle_Balance}" Margin="5,5,5,5" d:LayoutOverrides="Height"/>
                <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" x:Name ="LB_CheckName"  Text="{TemplateBinding Tag}"   Style="{DynamicResource LB_AreaTitle_Balance}" Margin="5,5,5,5"/>
    <Border x:Name="Divide" BorderBrush="Gray" BorderThickness="0,0.5" Height="140" Width="Auto" Margin="18.5,0" VerticalAlignment="Bottom"/>
    <Trigger Property="IsKeyboardFocused" Value="True"/>

    <Trigger Property="IsEnabled" Value="false">
        <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>

** I have list box of such items. I want to bind it to an observable list. the problem is that the previus programer added items to the list like this:**

  private void AddButtonToList(String LB_CheckName, String LB_Date, String LB_TchName, String LB_OK, String LB_Result, enmTechMode_Check enmCheck )
            //create item
            ListBoxItem item2 = new ListBoxItem();
            //set template
            item2.SetResourceReference(ListBoxItem.TemplateProperty, "tmpl_btn_TecnicianModeMenu");

            item2.Height = 45;

            TextBlock txt1 = (TextBlock)item2.Template.FindName("LB_CheckName", item2); 
            txt1.Text = LB_CheckName;

            txt1 = (TextBlock)item2.Template.FindName("LB_Date", item2); 
            txt1.Text = LB_Date;

            txt1 = (TextBlock)item2.Template.FindName("LB_TchName", item2);  
            txt1.Text = LB_TchName;

            txt1 = (TextBlock)item2.Template.FindName("LB_OK", item2);   
            txt1.Text = LB_OK;

            txt1 = (TextBlock)item2.Template.FindName("LB_Result", item2);   
            txt1.Text = LB_Result;

            Button bt = (Button)item2.Template.FindName("button", item2);
            bt.SetResourceReference(Button.StyleProperty, "ButtonListBoxItem");

            bt.Click += new RoutedEventHandler(Item_Selected);
            //set tag
            bt.Tag = enmCheck;

            //add to list
            StackPanel sp = (StackPanel)ListBoxData.FindName("stackPanel");
            item2.Tag = enmCheck;

        catch (Exception exp)

and I have no clue how to fix this

I assume theres suppose to be a use of a convertor? please provide me a direction

I assume the observable list suppose to be of structs. but how convert those to items in the temples format?

2012-04-04 06:55
by Nahum


In a template you would usually bind to properties of the data object. If for example you have the data class below with a Result property

public class MyData
    public string Result { get; set; }

you would bind like this:

<TextBlock Text="{Binding Path=Result}" ... />

Then you would not manually add ListBoxItems, but instead add data objects to the ObservableCollection, that the ItemsSource property of the ListBox is bound to:

    new MyData
        Result = "A Result"

where myDataObjects is an ObservableCollection<MyData>.

In case you would need to get the UI updated when a data object changes which is already contained in the ObservableCollection, class MyData would need to implement INotifyPropertyChanged.

2012-04-04 07:51
by Clemens
what format myData would be? struct - Nahum 2012-04-04 08:41
Not quite sure what you mean. It would be a class with public properties, as shown in the answer - Clemens 2012-04-04 08:51
not dependecy properties - Nahum 2012-04-04 09:53
No, that's not necessary. Only if properties can change after the data object has been added to the collection, the data class could implement INotifyPropertyChanged and fire the PropertyChanged event - Clemens 2012-04-04 09:59