Note that there are some explanatory texts on larger screens.

plurals
  1. POadd custom control with image to wpf Listbox
    text
    copied!<p>I have this project where I would like to set some images to my screen by adding a custom control (inSignalLight) to a listbox (or some other control, I would just like to line them up). I have set the images in the custom controls and added them to a "ObservableCollection", but nothing shows up. Im really new to WPF, so not quite sure if the xaml is corrent... If there is a better way to do it then in a listbox, please tell me so too. </p> <pre><code>inSignalLights = new ObservableCollection&lt;inSignalLight&gt;(); </code></pre> <p>Here is the xaml in the page that I would like to show the pictures in. </p> <pre><code>&lt;Page x:Class="Project.Pages.MainPicView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Beige" Title="MainPicView" d:DesignHeight="343" d:DesignWidth="676"&gt; &lt;Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt; &lt;Label Content="Label" Height="30" HorizontalAlignment="Left" Margin="61,195,0,0" Name="label1" VerticalAlignment="Top" Width="164" /&gt; &lt;ListBox ItemsSource="{Binding Path=inSignalLights}" Width="400" Height="25" Margin="0,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"&gt; &lt;ListBox.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;WrapPanel IsItemsHost="True"/&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ListBox.ItemsPanel&gt; &lt;/ListBox&gt; &lt;/Grid&gt; &lt;/Page&gt; </code></pre> <p>Edit:</p> <p>This is the xaml for the custom control</p> <pre><code>&lt;UserControl x:Class="Project.CustomControls.inSignalLight" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="16" d:DesignWidth="16"&gt; &lt;Grid&gt; &lt;Image Height="16" HorizontalAlignment="Left" Margin="0,0,0,0" Name="signalImage" Stretch="Fill" VerticalAlignment="Top" Width="16" /&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>Edit:</p> <pre><code>for (int i = 0; i &lt; inpins; i++) { InPin ip = iFace.getInPin(i); parent.insertNewSignalLight(ip); } public void insertNewSignalLight(InPin ip) { inSignalLight isl = new inSignalLight(ip); isl.setLightOff(); this.inSignalLights.Add(isl.signalImage); } public void setLightOff() { setThreadSafeImage(signalImage); } private void gotLightSignal(InPin pin, EventArgs e) { Thread.CurrentThread.Join(200); if (pin.PinState == 1) setLightOn(); else setLightOff(); } public void setThreadSafeImage(Image iS) { string strUri2 = String.Format(@"pack://application:,,,/;component/Images/Signal_Gron_16.png"); BitmapImage img = new BitmapImage(new Uri(strUri2)); img.Freeze(); iS.Dispatcher.BeginInvoke( DispatcherPriority.Background, new Action(() =&gt; iS.Source = img)); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload