I have the following templete
<ControlTemplate x:Key="tmpl_btn_TecnicianModeMenu" TargetType="{x:Type ListBoxItem}">
<Grid Opacity="1" d:LayoutOverrides="Width, Height">
<Border
x:Name="Border"
CornerRadius="0"
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}"/>
</Button>
<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"/>
</StackPanel>
</StackPanel>
</Border>
<Border x:Name="Divide" BorderBrush="Gray" BorderThickness="0,0.5" Height="140" Width="Auto" Margin="18.5,0" VerticalAlignment="Bottom"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="True"/>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
** 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 )
{
try
{
//create item
ListBoxItem item2 = new ListBoxItem();
//set template
item2.SetResourceReference(ListBoxItem.TemplateProperty, "tmpl_btn_TecnicianModeMenu");
item2.ApplyTemplate();
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.ApplyTemplate();
bt.Click += new RoutedEventHandler(Item_Selected);
//set tag
bt.Tag = enmCheck;
//add to list
StackPanel sp = (StackPanel)ListBoxData.FindName("stackPanel");
item2.Tag = enmCheck;
sp.Children.Add(item2);
}
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?
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:
myDataObjects.Add(
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.
PropertyChanged
event - Clemens 2012-04-04 09:59