Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a C# WPF Image Button with a template that I can alter in code dynamically
    primarykey
    data
    text
    <p>Hey, I was figuring out how to create an image button template in C# WPF, and I came upon <a href="https://stackoverflow.com/questions/1261908/wpf-how-to-create-image-button-with-template">this</a> . This seems really useful to me except for two problems:</p> <ol> <li>I want to be able to change the images dynamically in the C# codebehind, as this button will be using images that I am loading dynamically through XML documents.</li> <li><strong>FIXED</strong> I'm getting the error that "The name 'InitializeComponent' does not exist in the current context". Here is my code (note, I am not having a disabled image, only a pressed and a normal. the Normal.png and the other one are just placeholders.</li> </ol> <p>I was hoping to use this in a few places, but I can always create a few different versions. However, I need the images to load dynamically and I'm not able to find out how to make an image I specify in my C# codebehind basically act as a button. Thanks</p> <p><strong>NOTE: I'm able to get the button to work well, except for the changing the image source part. this code is what I have now.</strong></p> <pre><code> &lt;Button x:Class="CFYLauncher.PageNavButton" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt; &lt;Button.Template&gt; &lt;ControlTemplate TargetType="{x:Type Button}"&gt; &lt;Grid&gt; &lt;Image x:Name="NormalImg" Source="\img\btn_arrow.png"/&gt; &lt;Image x:Name="PressedImg" Source="\img\btn_arrow_selected.png" Visibility="Hidden"/&gt; &lt;/Grid&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="IsPressed" Value="True"&gt; &lt;Setter TargetName="NormalImg" Property="Visibility" Value="Hidden"/&gt; &lt;Setter TargetName="PressedImg" Property="Visibility" Value="Visible"/&gt; &lt;/Trigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; &lt;/Button.Template&gt; &lt;/Button&gt; </code></pre> <p>with codebehind </p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; 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.Navigation; using System.Windows.Shapes; namespace CFYLauncher { /// &lt;summary&gt; /// Interaction logic for PageNavButton.xaml /// &lt;/summary&gt; public partial class PageNavButton : Button { public PageNavButton() { InitializeComponent(); } public string ImageUri { set { this.NormalImg.Source = new BitmapImage(value); } } } } </code></pre> <p>As stated below in the reply to Tom Carver's help, I'm getting two major problems with this code:</p> <ol> <li>"Error 1 'CFYLauncher.PageNavButton' does not contain a definition for 'NormalImg' and no extension method 'NormalImg' accepting a first argument of type 'CFYLauncher.PageNavButton' could be found (are you missing a using directive or an assembly reference?) </li> <li>"Error 2 The best overloaded method match for 'System.Windows.Media.Imaging.BitmapImage.BitmapImage(System.Uri)' has some invalid arguments" and "Cannot convert from 'string' to 'System.Uri'</li> </ol> <p>When I change 'public string ImageUri' to 'public Uri ImageUri' the 2 errors in 2) go away, but it still can't find NormalImg, which doesn't make sense to me, because it's right there.. I must be missing something really obvious</p>
    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.
 

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