Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A couple of points:<br/> 1) Don't get your UI mixed in with your business logic.<br/> 2) You can build a better solution with a collection and a DataTemplate, something like this:</p> <p>C#</p> <pre><code>public class ThingType : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged = delegate { }; public string ThingName { get; set; } private bool thingAvailable; public bool ThingAvailable { get { return thingAvailable; } set { thingAvailable = value; PropertyChanged(this, new PropertyChangedEventArgs("ThingAvailable")); } } } public class MainVM { public ObservableCollection&lt;ThingType&gt; MyThings { get; set; } public MainVM() { MyThings = new ObservableCollection&lt;ThingType&gt;(); MyThings.Add(new ThingType() { ThingName = "thing 1" }); MyThings.Add(new ThingType() { ThingName = "thing 2" }); } public ThingType GetRandomThing() { var availableThings = MyThings.Where(x =&gt; x.ThingAvailable == true).ToArray(); int randomThingIndex = new Random().Next(availableThings.Length - 1); availableThings[randomThingIndex].ThingAvailable = false; return availableThings[randomThingIndex]; } } </code></pre> <p>XAML</p> <pre><code>&lt;Window x:Class="WpfApplication2.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;Window.Resources&gt; &lt;DataTemplate x:Key="thingTemplate"&gt; &lt;CheckBox Content="{Binding ThingName}" IsChecked="{Binding ThingAvailable, Mode=TwoWay}"/&gt; &lt;/DataTemplate&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;ListBox Height="100" HorizontalAlignment="Left" Margin="135,56,0,0" Name="listBox1" VerticalAlignment="Top" ItemsSource="{Binding MyThings}" ItemTemplate="{StaticResource thingTemplate}" Width="120" /&gt; &lt;Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="88,195,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    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