Hello,

I need for my program a listbox with itemtemplate, which can be loaded from xml file and also can be saved to xml file.

Itemtemplate for my is:

<ListBox.ItemTemplate>
<DataTemplate >
<Border Name="BD" BorderBrush="#FF305F86" Margin="3" Padding="3" BorderThickness="2" Background="AliceBlue"> <!--Background="#FFE8EBEC"-->
<StackPanel Orientation="Vertical">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60" />
<ColumnDefinition Width="130" />
<ColumnDefinition Width="125"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="110" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="110" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Width="50" FontSize="12" Content="Name: " HorizontalAlignment="Left"/>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=Name}" Name="txtName" Height="24" Width="120" FontSize="12" HorizontalAlignment="Left" />
<Label Grid.Row="0" Grid.Column="1" Name="lbName" FontWeight="Bold" Height="24" Width="120" FontSize="12" HorizontalAlignment="Left" Visibility="Hidden" />
<Label Grid.Row="0" Grid.Column="2" Width="105" Height="24" FontSize="12" Content="Comparison Type: " HorizontalAlignment="Right"/>
<ComboBox Name="cbConfType" Grid.Row="0" Grid.Column="3" SelectedIndex="{Binding Path=Index}" Width="120" Height="24" FontSize="12" SelectionChanged="ComboBox_SelectionChanged" Loaded="ComboBox_OnLoaded" HorizontalAlignment="Left" />
<Label Name="lbConfType" Grid.Row="0" Grid.Column="3" Visibility="Hidden" Width="120" Height="24" FontSize="12" FontWeight="Bold" HorizontalAlignment="Right" />
<Label Name="lbTolerance" Grid.Row="0" Grid.Column="4" Height="24" Width="70" FontSize="12" Visibility="Hidden" Content="Tolerance: " HorizontalAlignment="Right"/>
<TextBox Name="txtTolerance" Grid.Row="0" Grid.Column="5" Text="{Binding Path=Tolerance}" Height="24" Width="40" FontSize="12" Visibility="Hidden" HorizontalAlignment="Left" />
<Label Name="lbToleranceValue" Grid.Row="0" Grid.Column="5" FontWeight="Bold" Height="24" Width="40" FontSize="12" Visibility="Hidden" HorizontalAlignment="Left" />
<Label Name="lbMinValue" Grid.Row="0" Grid.Column="4" Height="24" Width="100" FontSize="12" Visibility="Hidden" Content="Minimum Value:" HorizontalAlignment="Right"/>
<TextBox Name="txtMinValue" Grid.Row="0" Grid.Column="5" Text="{Binding Path=MinValue}" Height="24" Width="40" FontSize="12" Visibility="Hidden" HorizontalAlignment="Left" />
<Label Name="lbMinimumValue" Grid.Row="0" Grid.Column="5" FontWeight="Bold" Height="24" Width="40" FontSize="12" Visibility="Hidden" HorizontalAlignment="Left" />
<Label Name="lbMaxValue" Grid.Row="0" Grid.Column="6" Height="24" Width="100" FontSize="12" Content="Maximum Value: " Visibility="Hidden" HorizontalAlignment="Right" />
<TextBox Name="txtMaxValue" Grid.Row="0" Grid.Column="7" Height="24" Width="40" Text="{Binding Path=MaxValue}" FontSize="12" Visibility="Hidden" HorizontalAlignment="Left" />
<Label Name="lbMaximumValue" Grid.Row="0" Grid.Column="7" FontWeight="Bold" Height="24" Width="40" FontSize="12" Visibility="Hidden" HorizontalAlignment="Left" />
</Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="620" />
<ColumnDefinition Width="90" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Height="24" Grid.Column="0" Width="60" FontSize="12" Content="Query: " HorizontalAlignment="Left"/>
<TextBox Name="txtQuery" Grid.Row="0" Height="24" Text="{Binding Path=Query}" Grid.Column="1" Width="585" FontSize="12" HorizontalAlignment="Left" />
<Label FontWeight="Bold" Name="lbQuery" Grid.Row="0" Height="24" Grid.Column="1" Width="650" FontSize="12" HorizontalAlignment="Left" />
<Button Grid.Row="0" Height="24" Grid.Column="3" Width="50" Content ="SAVE" HorizontalAlignment="Right" Click="btnSave_Click" />
</Grid>
</StackPanel>
</Border>
</DataTemplate>


This itemtemplate has two "modes". One for edit (when it is turned on textboxes are visible) and another one for save (labels are visible). I have just coped with saving. I wrote function, which sets suitable controls. And also this function saves all date from an item to an object. (A collection of these objects is exporting to xml file).

//This is a begging of this sub
If ItemsListBox.ItemContainerGenerator.Status = System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated Then

For Each listBoxItem As Object In ItemsListBox.Items
If listBoxItem Is ItemsListBox.SelectedItem Then
Dim container As ListBoxItem = TryCast(ItemsListBox.ItemContainerGenerator.ContainerFromItem(listBoxItem), ListBoxItem)
Dim listBoxItemBorder As Border = TryCast(VisualTreeHelper.GetChild(container, 0), Border)
Dim presenter As ContentPresenter = TryCast(VisualTreeHelper.GetChild(listBoxItemBorder, 0), ContentPresenter)
Dim bd As Border = TryCast(container.ContentTemplate.FindName("BD", presenter), Border)



Now, I have problem with load. I cannot do it in the same way as "save". I have made up a (stupid ) solution. I added to itemtemplate's field a binding. (Item is load from an object). Despite, it is stupid, it also does not work! Because of combobox. I have a function Combobox_onLoad, which fills this control. The problem is, Combobox_onLoad is called after binding and I always have the same value, which is not always right.

Please advice me, what should I do. It is possible to do this listbox in this way? Maybe, I should create two itemtemplates for listbox?

Thank you in advice