Click here to Skip to main content
12,996,261 members (66,171 online)
Rate this:
Please Sign up or sign in to vote.
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*"/>
            <RowDefinition Height="427*"/>
            <RowDefinition Height="15*"/>
            <RowDefinition Height="0*"/>
        <TextBox x:Name="tbSearch" HorizontalAlignment="Left" Height="23" Margin="10,41,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" Grid.ColumnSpan="2"/>
        <Label x:Name="lblSearch" Content="Vinyl Search" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" FontSize="16" FontWeight="Bold" Height="31" Width="102" Grid.ColumnSpan="2"/>
        <Button x:Name="btnSearch" Content="Search" HorizontalAlignment="Left" Margin="19,69,0,0" VerticalAlignment="Top" Width="75" Click="btnSearch_Click" Height="22" Grid.ColumnSpan="2"/>
        <ComboBox x:Name="cbVinylResults" HorizontalAlignment="Left" Margin="204.5,41,0,0" VerticalAlignment="Top" Width="200" Height="22" Grid.Column="1"/>
        <Label x:Name="lblVinylResults" Content="Vinyl Results" HorizontalAlignment="Left" Margin="204.5,15,0,0" VerticalAlignment="Top" Height="26" Width="77" Grid.Column="1"/>
        <Label x:Name="lblName" Content="Name" HorizontalAlignment="Left" Margin="10,96,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51" Height="26" Width="42" Grid.ColumnSpan="2"/>
        <Label x:Name="lblVinylLabel" Content="Vinyl Label" HorizontalAlignment="Left" Margin="10,155,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51" Height="26" Width="67" Grid.ColumnSpan="2"/>
        <Label x:Name="lblGenre" Content="Genre" HorizontalAlignment="Left" Margin="10,214,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51" Height="26" Width="42" Grid.ColumnSpan="2"/>
        <Label x:Name="lblSubGenre" Content="Sub Genre" HorizontalAlignment="Left" Margin="10,273,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51" Height="26" Width="65" Grid.ColumnSpan="2"/>
        <Label x:Name="lblImageUpload" Visibility="Hidden" Content="Upload Image" HorizontalAlignment="Right" Margin="0,284,90,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51" Width="337" Height="26" Grid.Column="1"/>
        <Label x:Name="lblUrl" Content="Website" Margin="10,332,400,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51" Height="26" Grid.ColumnSpan="2"/>
        <Image x:Name="imgPreview" Height="163" Margin="184,164,106,0" VerticalAlignment="Top" Grid.Column="1"/>
        <Label x:Name="lblImage" Content="Image Preview" HorizontalAlignment="Left" Margin="183.5,133,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.245,0.51" Height="26" Width="87" Grid.Column="1"/>
        <Button x:Name="btnAllVinyl" Content="Show" HorizontalAlignment="Left" Margin="329.5,69,0,0" VerticalAlignment="Top" Width="75" Click="btnAllVinyl_Click" Height="22" Grid.Column="1"/>
        <TextBox x:Name="tbName" Text="{Binding Name}" HorizontalAlignment="Left" Height="23" Margin="10,127,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2"/>
        <TextBox x:Name="tbLabel" HorizontalAlignment="Left" Height="23" Margin="10,186,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2"/>
        <TextBox x:Name="tbGenre" HorizontalAlignment="Left" Height="23" Margin="10,245,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2"/>
        <TextBox x:Name="tbSubGenre" HorizontalAlignment="Left" Height="23" Margin="10,304,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2"/>
        <TextBox x:Name="tbLabel_Copy" HorizontalAlignment="Left" Height="23" Margin="10,363,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="318" Grid.ColumnSpan="2"/>
        <Button x:Name="btnOpenUrl" Content="Open Website" HorizontalAlignment="Left" Margin="301.5,364,0,0" VerticalAlignment="Top" Width="103" RenderTransformOrigin="0.21,0.023" Click="btnOpenUrl_Click" Height="22" Grid.Column="1"/>

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 20: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
Web02 | 2.8.170609.2 | Last Updated 24 Jun 2013
Copyright © CodeProject, 1999-2017
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