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

  • SEARCH
  • Home
  • 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 8868099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:11:08+00:00 2026-06-14T17:11:08+00:00

I am trying to do a small thing with DataBinding in WPF and C#,

  • 0

I am trying to do a small thing with DataBinding in WPF and C#, but I can’t understand any of the tutorials for DataBinding.. my problem is, that I have a character class in C# and it has X and Y Coordinates with get and set functions. Then I have an image in a canvas in the window and now I am trying to bind the coordinates from the character class to the image. (There is only one image, and there will be created only one instance of the character class, though not at the beginning). Hope anyone can explain it so that I can fully understand it :/

EDIT:

Here is the XAML:

<Window x:Class="ProjectChar.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="800" Width="1000" Background="Gray">
<Viewbox StretchDirection="Both" Stretch="Uniform">
    <Grid>
        <Canvas Name="Canv" Background="White" Visibility="Hidden" MaxHeight="750" MaxWidth="1000">
            <Canvas.LayoutTransform>
                <ScaleTransform ScaleY="-1"/>
            </Canvas.LayoutTransform>
            <Image Name="CharImage"  Source="CharBild.png" Canvas.Left="{Binding iXCoordinate}" Canvas.Top="{Binding iYCoordinate}"/>
        </Canvas>
    </Grid>
</Viewbox>

and here is the C#-Part:

namespace ProjectChar
{
    public class Char : MainWindow
    {
        public int iXCoordinate { get; set; }
        public int iYCoordinate { get; set; }
    }
}

So I somehow need to bind them together, but I don’t exactly know how. I know that I have to create a DataContext and that I need to set the UpdateSource to PropertyChanged and that I need an EventHandler for the Property Changed, but I don’t know how to do any of that and the tutorials on the internet are all saying kind of different things :/

  • 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-06-14T17:11:09+00:00Added an answer on June 14, 2026 at 5:11 pm

    To bind the Image‘s X and Y coordinates to your properties, do this:

    1. Remove Char‘s inheritance from MainWindow.
    2. Set MainWindow‘s DataContext to an instance of Char.
    3. Remove Visibility="Hidden" from the Canvas

    Example:

    public class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new Char { iXCoordinate = 20, iYCoordinate = 50 };
        }
    }
    

    Make sure you actually set the iXCoordinate and iYCoordinate properties to something. If not, they will be the default value of int, which is 0. E.g. the image will show in the upper left corner.

    To let the View (UI) know when a bound value have changed, implement INotifyPropertyChanged and raise the PropertyChanged event every time a property is set:

    public class Char : INotifyPropertyChanged
    {
        private int _iXCoordinate;
    
        public int iXCoordinate
        {
            get { return _iXCoordinate; }
            set
            {
                _iXCoordinate = value;
                OnPropertyChanged("iXCoordinate");
            }
        }
    
        private int _iYCoordinate;
    
        public int iYCoordinate
        {
            get { return _iYCoordinate; }
            set
            {
                _iYCoordinate = value;
                OnPropertyChanged("iYCoordinate");
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    This is the most simple way to implement INotifyPropertyChanged. However, I would recommend to make a base class which implements the interface and let your ViewModels inherit from that. Try something like this:

    public class PropertyChangedBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected void OnPropertyChanged<T>(Expression<Func<T>> propertyExpression)
        {
            var handler = PropertyChanged;
            var propertyName = GetPropertyName(propertyExpression);
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    
        private static string GetPropertyName<T>(Expression<Func<T>> propertyExpression)
        {
            var memberExpression = propertyExpression.Body as MemberExpression;
            if (memberExpression == null)
            {
                var unaryExpression = propertyExpression.Body as UnaryExpression;
                if (unaryExpression == null) 
                    throw new ArgumentException("Expression must be a MemberExpression.", "propertyExpression");
    
                memberExpression = unaryExpression.Operand as MemberExpression;
            }
    
            if (memberExpression == null) 
                throw new ArgumentException("Expression must be a MemberExpression.", "propertyExpression");
    
            var propertyInfo = memberExpression.Member as PropertyInfo;
            if (propertyInfo == null) 
                throw new ArgumentException("Expression must be a Property.", "propertyExpression");
    
            return propertyInfo.Name;
        }
    }
    

    This PropertyChangedBase also has the added benefit that you have to raise the PropertyChanged event by using a lambda expression instead of using “magic strings”, which is prone to typos and bad for refactoring:

        public int iYCoordinate
        {
            get { return _iYCoordinate; }
            set
            {
                _iYCoordinate = value;
                OnPropertyChanged(() => iYCoordinate);
            }
        }
    

    However, your REAL problem is the ViewBox which wraps the Grid.

    A ViewBox measures it’s children to a size of Infinity, it then arranges them by their DesiredSize. This means that it’s children can be whatever size they want to be. Both Grid and Canvas have no size they want to be, they rely on their parent to give them a size.

    To solve your problem, specify Width and Height of either the Grid or the Canvas inside it.

    BTW: You know that

    <Canvas.LayoutTransform>
      <ScaleTransform ScaleY="-1"/>
    </Canvas.LayoutTransform>
    

    will flip your cavas, right?

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

Sidebar

Related Questions

I am not trying to do any such thing, but I was wondering out
I am trying to make a small HTML-wysiwyg with a JTextPane but I can't
Good day, I'm trying to figure out how to do small thing. I have
I have a small program that which is confusing me. I am trying using
It's a small thing, really: I have this function that converts dict objects to
i am trying to add small icon to VirtualTreeview in delphi2010 i have ImageList
Trying to load a small .txt file into mysql but get all my data
Trying to make a small countdown timer in my app but it's not working.
I am trying to write a small Java application that will let me run
I'm currently trying to make a small application that performs different duties. Right now

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.