Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The concepts covered are INotifyPropertyChanges (in .net 4.5 (minimal changes for other versions but same concept)), ItemsControl and finally style triggers. </p> <h2>INotifyPropertyChanges</h2> <p>My first step is to put INotifyPropertyChanges on our mainwindow to notify the WPF controls of any changes automatically. You will convert your bit array to a list and simply drop it in (maybe on a timer?) when needed. Note it doesn't matter how many there are...the control will expand.</p> <pre><code>public partial class MainWindow : Window, INotifyPropertyChanged { private List&lt;bool&gt; _LedStates; public List&lt;bool&gt; LedStates { get { return _LedStates; } set { _LedStates = value; NotifyPropertyChanged(); } } public MainWindow() { InitializeComponent(); DataContext = this; LedStates = new List&lt;bool&gt;() {true, false, true}; } #region INotifyPropertyChanged /// &lt;summary&gt; /// Event raised when a property changes. /// &lt;/summary&gt; public event PropertyChangedEventHandler PropertyChanged; /// &lt;summary&gt; /// Raises the PropertyChanged event. /// &lt;/summary&gt; /// &lt;param name="propertyName"&gt;The name of the property that has changed.&lt;/param&gt; protected virtual void NotifyPropertyChanged( [CallerMemberName] String propertyName = "" ) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler( this, new PropertyChangedEventArgs( propertyName ) ); } } #endregion } </code></pre> <h2>ItemsControl &amp; Style Triggers</h2> <p>Then in the WPF xaml we will bind to the list of bools and use an item template (think a generic mini view for each data item found). Within that template we will show a color of red or green depending on the state of the boolean. </p> <p>No converter needed because we setup a style trigger which is attune to a target value. If the Textblocks text is "True" we will show Green and when "False" we will trigger/show red.</p> <pre><code>&lt;Window x:Class="WPFBindToArray.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;ItemsControl x:Name="icBitViewer" ItemsSource="{Binding LedStates}"&gt; &lt;ItemsControl.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;StackPanel HorizontalAlignment="Stretch" IsItemsHost="True" /&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ItemsControl.ItemsPanel&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding}" Grid.Column="0"&gt; &lt;TextBlock.Style&gt; &lt;Style TargetType="{x:Type TextBlock}"&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="Text" Value="True"&gt; &lt;Setter Property="Background" Value="Green" /&gt; &lt;/Trigger&gt; &lt;Trigger Property="Text" Value="False"&gt; &lt;Setter Property="Background" Value="Red" /&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/TextBlock.Style&gt; &lt;/TextBlock&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>When this program is run here is the result:</p> <p><img src="https://i.stack.imgur.com/1C5X5.png" alt="enter image description here"></p>
 

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