Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok. Delete all your code and start all over.</p> <p>The default approach to WPF (regardless of the application type, a game in this case) is something called <a href="https://stackoverflow.com/questions/14381402/wpf-programming-methodology/14382137#14382137">MVVM</a> which I will go as far as to say you must learn before you ever write a single line of code or XAML in WPF.</p> <p>This is my take on what you described:</p> <pre><code>&lt;Window x:Class="MiscSamples.WhackAMole" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WhackAMole" Height="300" Width="300"&gt; &lt;ItemsControl ItemsSource="{Binding Moles}"&gt; &lt;ItemsControl.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;UniformGrid Rows="3" Columns="3" IsItemsHost="True"/&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ItemsControl.ItemsPanel&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Image x:Name="Mole" Height="0" Width="100" Source="/Images/Mole.Png" Stretch="Fill" VerticalAlignment="Bottom"/&gt; &lt;DataTemplate.Triggers&gt; &lt;DataTrigger Binding="{Binding IsUp}" Value="True"&gt; &lt;DataTrigger.EnterActions&gt; &lt;BeginStoryboard&gt; &lt;Storyboard TargetName="Mole"&gt; &lt;DoubleAnimation Storyboard.TargetProperty="Height" From="0" To="77" Duration="00:00:00.3"/&gt; &lt;/Storyboard&gt; &lt;/BeginStoryboard&gt; &lt;/DataTrigger.EnterActions&gt; &lt;DataTrigger.ExitActions&gt; &lt;BeginStoryboard&gt; &lt;Storyboard TargetName="Mole"&gt; &lt;DoubleAnimation Storyboard.TargetProperty="Height" From="77" To="0" Duration="00:00:00.3"/&gt; &lt;/Storyboard&gt; &lt;/BeginStoryboard&gt; &lt;/DataTrigger.ExitActions&gt; &lt;/DataTrigger&gt; &lt;/DataTemplate.Triggers&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; &lt;/Window&gt; </code></pre> <p>Code Behind:</p> <pre><code>public partial class WhackAMole : Window { public WhackAMole() { InitializeComponent(); DataContext = new WhackAMoleViewModel(); } } </code></pre> <p>ViewModel:</p> <pre><code>public class WhackAMoleViewModel: PropertyChangedBase { private List&lt;Mole&gt; _moles; public List&lt;Mole&gt; Moles { get { return _moles; } } private System.Threading.Timer timer; private System.Random random = new Random(); public WhackAMoleViewModel() { _moles = Enumerable.Range(1, 9).Select(x =&gt; new Mole()).ToList(); timer = new Timer(x =&gt; RaiseRandomMole(), null, 0, 300); } private void RaiseRandomMole() { //If random number is less than 5 skip this iteration if (random.Next(1, 10) &gt; 5) return; //Choose a random mole var mole = Moles[random.Next(0, 8)]; //If it's already raised, do nothing if (mole.IsUp) return; //Raise it mole.IsUp = true; //Then Get it down somewhere between 1 and 2 seconds after Task.Factory.StartNew(() =&gt; Thread.Sleep(random.Next(1000, 2000))) .ContinueWith(x =&gt; mole.IsUp = false); } } </code></pre> <p>Data Item:</p> <pre><code>public class Mole: PropertyChangedBase { private bool _isUp; public bool IsUp { get { return _isUp; } set { _isUp = value; OnPropertyChanged("IsUp"); } } } </code></pre> <p>PropertyChangedBase class (MVVM helper):</p> <pre><code>public class PropertyChangedBase:INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { Application.Current.Dispatcher.BeginInvoke((Action) (() =&gt; { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); })); } } </code></pre> <p>Result:</p> <p><img src="https://i.stack.imgur.com/XAi79.png" alt="enter image description here"></p> <ul> <li>Notice that I'm using WPF's animations capabitilites to show / hide the moles.</li> <li>Also notice that there's not a single line of code in my example that references or manipulates any UI elements. The UI is done in XAML and it reacts to changes in the model / ViewModel via <a href="http://msdn.microsoft.com/en-us/library/ms752347.aspx" rel="nofollow noreferrer">DataBinding</a></li> <li>I made a basic example, it doesn't support clicking on the moles, you will have to use a <a href="http://wpftutorial.net/DelegateCommand.html" rel="nofollow noreferrer"><code>DelegateCommand</code></a> for that.</li> <li>Build on my example and follow my logic (which is the right one). Don't manipulate UI elements in code.</li> </ul>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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