Click here to Skip to main content
11,934,783 members (47,109 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# WPF CodeProject convert Text , +
Good afternoon,

I have finished my project and it is working, however. I need to change it to use the binding property of a textbox, instead of assigning the text manually, in my code behind. Below is my Xaml and my code behind. Any advice on how to change my current working project into using Xaml bindings, would be greatly appreciated.

P.s My Project is using Codefirst to create and use the sql database.

Here is my Xaml:

<UserControl x:Class="VinylCollection.ucViewVinyl"
             mc:Ignorable="d" d:DesignWidth="529.5" Height="1015.5">
    <Grid Margin="0,0,0,318">
            <ColumnDefinition Width="32*"/>
            <ColumnDefinition Width="499*"/>

Here is my code behind:

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.Drawing;
using System.Drawing.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataAccess;
using BusinessModel;
using System.Data.EntityModel;
using System.Data.Sql;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
namespace VinylCollection
    /// <summary>
    /// Interaction logic for ucViewVinyl.xaml
    /// </summary>
    public partial class ucViewVinyl : UserControl
        #region Constructors

        public ucViewVinyl()

        #region Variable

        byte[] newImage;

        #region Methods

        // Creates an instance of the VinylContext class and gets the results based on the tbSearch.Text
        // Adds the results as an item to the combobox.  

        public void SearchVinyl()
           using (var vinylContext = new VinylContext())
               var query =  from b in vinylContext.VinylSet
                           orderby b.VinylId
                           select b;

               foreach (var item in query.Where(v => v.Name == tbSearch.Text))

        // Creates an instance of VinylContext class and gets the results based on the combobox.SelectedItem.
        // Adds the results into all the label.content and image.source.

        public void SelectVinyl()
            using (var vinylContext = new VinylContext())
                var query = from b in vinylContext.VinylSet
                            orderby b.VinylId
                            select b;

                foreach (var item in query.Where(v => v.Name ==  cbVinylResults.SelectedItem))
                    lblName.Content = item.Name;
                    lblVinylLabel.Content = item.VinylLabel;
                    lblGenre.Content = item.Genre;
                    lblSubGenre.Content = item.SubGenre;
                    lblUrl.Content = item.Url;
                    lblImageUpload.Content = item.Photo;
                    newImage = item.Photo;
                    imgPreview.Source = ByteImageConverter.ByteToImage(newImage);


        #region Class

        // Creates an instance of ByteImageConverter, and uses the ByteToImage method to create an image from the byte array.

        public class ByteImageConverter
            public static ImageSource ByteToImage(byte[] imageData)
                BitmapImage biImg = new BitmapImage();
                MemoryStream ms = new MemoryStream(imageData);
                biImg.StreamSource = ms;
                ImageSource imgSrc = biImg as ImageSource;
                return imgSrc;

            public static string ImageToByte(FileStream fs)
                byte[] imgBytes = new byte[fs.Length];
                fs.Read(imgBytes, 0, Convert.ToInt32(fs.Length));
                string encodeData = Convert.ToBase64String(imgBytes, Base64FormattingOptions.InsertLineBreaks);
                return encodeData;


        #region Events

        private void btnSearch_Click(object sender, RoutedEventArgs e)

        private void btnAllVinyl_Click(object sender, RoutedEventArgs e)
        private void btnOpenUrl_Click(object sender, RoutedEventArgs e)

Any help would be greatly appreciated, thank you.


Posted 23-Jun-13 21:37pm
Alexander Dymshyts 24-Jun-13 3:28am
You are trying to bind a property Name, but you didn't defind it in code behind. So you should create a property,and use INotifyPropertyChanged interface to notify clients that a property value has changed.
deanmichaelcook 24-Jun-13 3:48am
I am trying to use the binding property of a textbox. If you could provide example code, or try to make your reply more readable/understandable.

Alexander Dymshyts 24-Jun-13 4:10am
if you have in xaml binding, for example - Text="{Binding Name}"
, you should write in code behind:
private string _name;
public string Name
get{ return _name; }
_name = value;
deanmichaelcook 24-Jun-13 4:19am
Ok, I will try this, however putting the RaisePropertyChange throws an error that it does not exist.
Alexander Dymshyts 24-Jun-13 4:22am
You should implement INotifyPropertyChanged interface
deanmichaelcook 24-Jun-13 4:23am
I'm sorry, I have never used that class before, could you provide a sample please?
Alexander Dymshyts 24-Jun-13 4:25am
Alexander Dymshyts 24-Jun-13 4:30am
Or may be this example will help you with your problem -
stibee 24-Jun-13 12:17pm
For your buttons I would recommend

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.1 | Last Updated 24 Jun 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100