0

I'm starting out on WPF and can't seem to find anything on this method. I'm most likely do not know the proper wording, so asking here.

I have a Button that I have formatted how I needed in XAML. I have it rendered in window properly, except the Image. Since the image will be called every time via code (as buttons will spawn as user needs them), the image was left blank in XAML.

In the Code portion, I'm having issues calling "imgIco" the name of the Image in the XAML Button Template created; "button.imgIco" does not seem to be working. ;)

Thank you in advance and here is the snippets of code interacting with each other.

XAML

<Setter.Value>
      <ControlTemplate TargetType ="{x:Type Button}">
        <Grid>
          <Ellipse Name ="UXbg" Fill ="Transparent" Stroke="White" StrokeThickness="1"/>
          <Ellipse Name ="UXbg2" Width="40" Height="40" Fill="White"/>
          <Image Name="UXid" Width="32" Height="32">
            <Image.Clip>
              <EllipseGeometry Center="16,16" RadiusX="16" RadiusY="16"/>
            </Image.Clip>
          </Image>
          <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Grid>
        <ControlTemplate.Triggers>
          <Trigger Property ="IsMouseOver" Value ="True">
            <Setter TargetName ="UXbg" Property ="Stroke" Value ="Orange"/>
            <Setter TargetName ="UXbg2" Property ="Fill" Value ="Orange"/>
          </Trigger>
          <Trigger Property ="IsPressed" Value ="True">
            <Setter TargetName ="UXbg" Property ="Stroke" Value ="White"/>
            <Setter TargetName ="UXbg2" Property ="Fill" Value ="White"/>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

C#.cs

ImageBrush iBrush = new ImageBrush();
  iBrush.ImageSource = new BitmapImage(new Uri(@"/Resources/32start.png", UriKind.Relative));
  aStart.UXid = iBrush;
unDrac
  • 13
  • 3
  • @eranotzap Seems to be the button. – Physikbuddha May 27 '15 at 06:54
  • Do you want to assign image to "UXid" element from `ControlTemplate`? If so, how `imgIco` is declared (assuming, that `aStart` is a button, styled with style you've posted). – Dennis May 27 '15 at 06:55
  • You should also take a look at how to load bitmap resources in code behind. [This answer](http://stackoverflow.com/a/15008178/1136211) may be helpful. – Clemens May 27 '15 at 07:46
  • @Physikbuddha i know it's the button. it was a question to make him realize something. and to make sure. – eran otzap May 27 '15 at 08:08
  • @unDrac listen , this doesn't seem like something that could even compile.. What do you think imgIco is ? – eran otzap May 27 '15 at 08:09
  • Sorry, The UXid / imgIco is a posting error. I have Notepad++ on my other monitor to view the CS while Designer is open and forgot to save when I cleaned up the variables for posting. >> aStart is the button. >> Yes, it compiles :( Going to update the post the clean it up, but the idea is to have a button template with an Image placeholder setup to be modified for when the buttons are generated for user Interaction. This way, the object is created and then the image is defined. I will try the answer from Clemens and Maxime, then post the results. Thank you everyone for all the help! :) – unDrac May 30 '15 at 06:23

1 Answers1

0

Your question seems to be missing some information but normally when you want to set an image with code behind, you can simply do it like this:

<Image Name="ModifiedImage" Initialized="ModifiedImage_Initialized"/>

code-behind:

private void ModifiedImage_Initialized(object sender, EventArgs e)
        {
            BitmapImage img = new BitmapImage();
            img.BeginInit();
            img.CacheOption = BitmapCacheOption.OnLoad;
            img.CreateOptions = BitmapCreateOptions.IgnoreImageCache;

            // TODO: check if the image exists, return a "image not found" image if not found.
            img.UriSource = new Uri(@"/Resources/32start.png", UriKind.Relative);
            img.EndInit();
            ((Image)sender).Source = img;
        }
  • This worked for rendering it outright. Turns out I need a better setup and will put in another question to reAddress the goal when I finalize the XAML Style. Thanks for everyone's help! – unDrac May 31 '15 at 06:22