Hi, I have created a custom ItemsControl called Toolbox. I want to be able to display images in that Toolbox - it is a part of a diagram designer.
My xaml looks like this:
<d:Toolbox ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Library}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</d:Toolbox>
and my ViewModel:
public ObservableCollection<ElectricalLibrary> l = null;
public ObservableCollection<Image> _images = null;
public ObservableCollection<Image> Library
{
get
{
if (l == null)
{
DataAccessLayerClass dc = new DataAccessLayerClass();
dc.LoadComponents();
l = dc.Library;
foreach (ElectricalLibrary lib in l) {
Image finalImage = new Image();
finalImage.Width = 80;
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri(lib.url.ToString());
logo.EndInit();
finalImage.Source = logo;
MessageBoxResult result = MessageBox.Show(logo.UriSource.ToString());
_images.Add(finalImage);
}
}
return _images;
}
set { _images = value; }
}
Ands this is a resource file for Toolbox itself:
<Style TargetType="{x:Type s:Toolbox}">
<Setter Property="SnapsToDevicePixels"
Value="true" />
<Setter Property="Focusable"
Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border BorderThickness="{TemplateBinding Border.BorderThickness}"
Padding="{TemplateBinding Control.Padding}"
BorderBrush="{TemplateBinding Border.BorderBrush}"
Background="{TemplateBinding Panel.Background}"
SnapsToDevicePixels="True">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel Margin="0,5,0,5"
ItemHeight="{Binding Path=DefaultItemSize.Height, RelativeSource={RelativeSource AncestorType=s:Toolbox}}"
ItemWidth="{Binding Path=DefaultItemSize.Width, RelativeSource={RelativeSource AncestorType=s:Toolbox}}" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
I store only the URLs of the images in the database, the images are stored on a disc. I take the entity object and create an image, add it into an ObservableCollection of images and bind Image control to LIbrary in xaml.
Obviously, the code does not work. But how to make it work? The list with images is loaded correctly.
Thanks for help.