Note that there are some explanatory texts on larger screens.

plurals
  1. POImage will not update in code only custom control inheriting from button in WPF
    primarykey
    data
    text
    <p>I am currently trying to build a custom button control for a dice game I am writing in WPF. Everything is working correctly except that the image in the UI while it is running does not change when the the die value has changed. I can confirm through my debug sessions that the image source is changing as expected as well as all values are changing as expected but the image just doesn't seem to want to update. I've tried looking over many similar questions but none of the questions seem to apply to my particular situation it would seem. I am new to WPF and not quite sure what I am doing wrong here... my current code is as follows </p> <pre><code>using BluffersDice.GameEngine; using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Imaging; namespace BluffersDice.Interface.CustomControls { public class DieButton : Button { private Die _DieValue; private readonly BitmapImage ONE_IMAGE = new BitmapImage(new Uri("/res/dice/1.png",UriKind.Relative)); private readonly BitmapImage TWO_IMAGE = new BitmapImage(new Uri("/res/dice/2.png", UriKind.Relative)); private readonly BitmapImage THREE_IMAGE = new BitmapImage(new Uri("/res/dice/3.png", UriKind.Relative)); private readonly BitmapImage FOUR_IMAGE = new BitmapImage(new Uri("/res/dice/4.png", UriKind.Relative)); private readonly BitmapImage FIVE_IMAGE = new BitmapImage(new Uri("/res/dice/5.png", UriKind.Relative)); private readonly BitmapImage SIX_IMAGE = new BitmapImage(new Uri("/res/dice/6.png", UriKind.Relative)); private const string HELD_LABEL_TEXT = "Held"; //private bool initcompleted = false; private Label HoldLabel { get; set; } public DieButton() : this(new Die()) { } public DieButton(Die dieValue) { Background = Brushes.Transparent; BorderBrush = new SolidColorBrush(Colors.Transparent); BorderThickness = new Thickness(6); HoldLabel = new Label() { MinHeight = 15 }; Click += This_OnClick; DieValueChanged += DieValueChangedHandler; dieValue.IsHeldChanged += DieValue_IsHeldChanged; dieValue.DieValueChanged += DieValueChangedHandler; _DieValue = dieValue; Panel = new StackPanel() { Orientation = Orientation.Vertical, Margin = new Thickness(8) }; DieImage = new Image() { Source = GetDieImageSource() }; Panel.Children.Add(DieImage); Panel.Children.Add(HoldLabel); Content = Panel; UpdateButtonContent(); //initcompleted = true; } private void This_OnClick(object sender, RoutedEventArgs e) { DieValue.ToggleHold(); } public event EventHandler DieValueChanged; public Die DieValue { get { return _DieValue; } set { _DieValue = value; if (DieValueChanged != null) { DieValueChanged(this, new EventArgs()); } } } private Image DieImage { get; set; } private StackPanel Panel { get; set; } private void DieValue_IsHeldChanged(object sender, EventArgs e) { var d = (Die)sender; if (d.IsHeld) { BorderBrush = new SolidColorBrush(Colors.Yellow); } else { BorderBrush = new SolidColorBrush(Colors.Transparent); } HoldLabel.Content = DieValue.IsHeld ? HELD_LABEL_TEXT : string.Empty; } private void DieValueChangedHandler(object sender, EventArgs e) { DieImage.Source = GetDieImageSource(); UpdateButtonContent(); } private ImageSource GetDieImageSource() { switch (DieValue.Value) { case 1: return ONE_IMAGE; case 2: return TWO_IMAGE; case 3: return THREE_IMAGE; case 4: return FOUR_IMAGE; case 5: return FIVE_IMAGE; case 6: return SIX_IMAGE; default: return null; } } private void UpdateButtonContent() { (Panel.Children[0] as Image).Source = GetDieImageSource(); } } } </code></pre> <p><strong>Window Is being used on:</strong></p> <pre><code>using BluffersDice.GameEngine; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using BluffersDice.Interface.CustomControls; namespace BluffersDice.Interface { /// &lt;summary&gt; /// Interaction logic for UserTurn.xaml /// &lt;/summary&gt; public partial class PlayerTurn : Window { public Turn TurnState { get; set; } private Roll CurrentRoll { get; set; } public PlayerTurn() { CurrentRoll = new Roll(); InitializeComponent(); btn_Die1 = new DieButton(CurrentRoll.Dice[0]); btn_Die2 = new DieButton(CurrentRoll.Dice[1]); btn_Die3 = new DieButton(CurrentRoll.Dice[2]); btn_Die4 = new DieButton(CurrentRoll.Dice[3]); btn_Die5 = new DieButton(CurrentRoll.Dice[4]); GameState.Caller.StartNewTurn(); TurnState = GameState.Caller.StartNewTurn(); lbl_PlayerTitle.Text = string.Format(lbl_PlayerTitle.Text, GameState.Caller.Id); } private void btn_DieValuechanged(object sender, EventArgs ea) { var d = (Die)sender; MessageBox.Show(String.Format("Die Button {0} Value Changed To {1}", d.Id, d.Value)); } private void DieValueChanged(object sender, EventArgs e) { var d = (Die)sender; //MessageBox.Show(String.Format("Die {0} Value Changed To {1}", d.Id, d.Value)); } private void RollDice_btnClick(object sender, RoutedEventArgs e) { CurrentRoll.RollDice(); } } } </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.
 

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