Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm sure you could figure out how to do what you're asking, but let's consider the tools at hand? You have an array of bool, it seems. As was suggested, an ItemsControl can handle them just wonderfully. First, let's do some code-behind to transform our bool's into brushes to set the background of our items.</p> <pre><code>using System; using System.Windows.Media; using System.Windows.Data; using System.Globalization; namespace MyNamespace { public class BoolToBrushConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { // return a Pink SolidColorBrush if true, a Maroon if false return (bool)value ? Brushes.Pink : Brushes.Maroon; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return (SolidColorBrush)value == Brushes.Pink; } } } </code></pre> <p>This will allow you to translate your <code>bool[] bitArray</code> into a series of brushes when bound to an <code>ItemsControl</code>. Now for some Xaml :</p> <p>First, make sure you declare your local namespace (which contains the converter we just defined) in the xmlns attributes as well as the System Core Library (see the xmlns attributes).</p> <pre><code>&lt;Window x:Class="MyNamespace.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" &lt;!-- our local namespace --&gt; xmlns:my="clr-namespace:MyNamespace" &lt;!-- system core library --&gt; xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="MainWindow" Height="600" Width="900"&gt; &lt;Grid&gt; &lt;ItemsControl Name="LEDPanel"&gt; &lt;!-- Need to Name this Control in order to set the ItemsSource Property at startup --&gt; &lt;ItemsControl.Resources&gt; &lt;my:BoolToBrushConverter x:Key="LEDConverter" /&gt; &lt;!-- Here we define our converter for use, note the preceding my: namespace declaration --&gt; &lt;/ItemsControl.Resources&gt; &lt;ItemsControl.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;StackPanel Orientation="Horizontal" /&gt; &lt;!-- this will make the items defined in the ItemTemplate appear in a row --&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ItemsControl.ItemsPanel&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate DataType="{x:Type sys:Boolean}"&gt; &lt;-- We will be binding our ItemsControl to a bool[] so each Item will be bound to a bool --&gt; &lt;Border Margin="3" CornerRadius="10" Height="20" Width="20" BorderThickness="2" BorderBrush="Silver" Background="{Binding Converter={StaticResource LEDConverter}}" /&gt; &lt;!-- This is where we describe our item. I'm drawing a round silver border and then binding the Background to the item's DataContext (implicit) and converting the value using our defined BoolToBrushConverter --&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>Edit: I forgot the DataBinding. In your Window's constructor:</p> <pre><code>public MainWindow() { InitializeComponent(); LEDPanel.ItemsSource = bitArray; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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