Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6255657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:24:23+00:00 2026-05-24T14:24:23+00:00

I am try to Make a Image Button User control. But When I set

  • 0

I am try to Make a Image Button User control. But When I set image at window Initialize then throw this exception. Here is my Code

EXAM.

<UserControl.Resources>
    <Style x:Key="ImageButton" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <StackPanel>
                        <Image x:Name="ButtonImage" Stretch="None"/>
                        <ContentPresenter Margin="0,8,0,0" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </StackPanel>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True"/>
                        <Trigger Property="IsDefaulted" Value="True"/>
                        <Trigger Property="IsMouseOver" Value="True"/>
                        <Trigger Property="IsPressed" Value="True"/>
                        <Trigger Property="IsEnabled" Value="False"/>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>
<Button x:Name="ProcestaImageButton" Content="TextBlock" Style="{DynamicResource ImageButton}"/>

C# Code.

    public ImageSource ButtonImage
    {
        get { return ((this.ProcestaImageButton.Template).FindName("ButtonImage", this.ProcestaImageButton) as Image).Source; }
        set { ((this.ProcestaImageButton.Template).FindName("ButtonImage", this.ProcestaImageButton) as Image).Source = value; }
    }

Please tell me where i am wrong.Please Provide me a Example.
Thank`s for Help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T14:24:23+00:00Added an answer on May 24, 2026 at 2:24 pm

    For a start, I’d expand that property out – each statement is rather complicated at the moment. Here’s the expanded version:

    public ImageSource ButtonImage
    {
        get
        { 
            var template = ProcestaImageButton.Template
                                    .FindName("ButtonImage", ProcestaImageButton);
    
            var image = template as Image;
            return image.Source;
        }
        set
        {
            var template = ProcestaImageButton.Template
                                    .FindName("ButtonImage", ProcestaImageButton);
            var image = template as Image;
            image.Source = value;
        }
    }
    

    I would then probably extract the common code to a helper property:

    private Image ProcestaImageButtonImage
    {
        get
        {
            var template = ProcestaImageButton.Template
                                    FindName("ButtonImage", ProcestaImageButton);
    
            return template as Image;
        }
    }
    
    public ImageSource ButtonImage
    {
        get { return ProcestaImageButtonImage.Source; }
        set { ProcestaImageButtonImage.Source = value; }
    }
    

    Next I would change the as to a cast:

    private Image ProcestaImageButtonImage
    {
        get
        {
            var template = ProcestaImageButton.Template
                                    .FindName("ButtonImage", ProcestaImageButton);
    
            return (Image) template;
        }
    }
    
    public ImageSource ButtonImage
    {
        get { return ProcestaImageButtonImage.Source; }
        set { ProcestaImageButtonImage.Source = value; }
    }
    

    At this point, you might still have a NullReferenceException or you might have a InvalidCastException – and suddenly you have a lot more information about what’s wrong.

    A cast is better here as you’re always expecting FindName to return an Image, aren’t you? If it’s an error condition for the type conversion to fail, you should use a cast – only use as when it’s valid for the expression not to be of the target type, and you’re intending to handle that case separately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to C# but I will try to make this quick! ;)
I'm trying to make things simpler. Here is my code: If Threading.Monitor.TryEnter(syncRoot) Then Try
Right now when I set an image over a button and make it border-less
I'll try to make this as straight forward as possible. Currently our team has
I'll try to make this as simple a posible. I just started working with
When I try to make a very large boolean array using Java, such as:
If we make any class in VB.NET and try to access its property in
is it possible to make beep in WinCE ? i try and i get
I have the following interface which I'm trying to make COM-visible. When I try
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.