Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 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:

C#
<UserControl x:Class="VinylCollection.ucViewVinyl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" d:DesignWidth="529.5" Height="1015.5">
    <Grid Margin="0,0,0,318">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="32*"/>
            <ColumnDefinition Width="499*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="427*"/>
            <RowDefinition Height="15*"/>
            <RowDefinition Height="0*"/>
        </Grid.RowDefinitions>
        <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"/>

    </Grid>
</UserControl>


Here is my code behind:

C#
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

       

        #endregion
 

        public ucViewVinyl()
        {
            InitializeComponent();
        }


        #region Variable

        byte[] newImage;

        #endregion


        #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))
               {
                   cbVinylResults.Items.Add(item.Name);
               }

           }
       }


        // 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))
                {
                    cbVinylResults.Items.Add(item.Name);
                    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);
                }
            }
        }
  

        #endregion



        #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.BeginInit();
                biImg.StreamSource = ms;
                biImg.EndInit();

                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;
            }
        }
        
        #endregion



        #region Events

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            SearchVinyl();
        }

        #endregion

        private void btnAllVinyl_Click(object sender, RoutedEventArgs e)
        {
            SelectVinyl();
        }

        private void btnOpenUrl_Click(object sender, RoutedEventArgs e)
        {

        }

        
    }
}



Any help would be greatly appreciated, thank you.

Regards

Dean
Posted
Comments
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.
TeacherDean 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.

Regards
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; }
set
{
_name = value;
RaisePropertyChange("Name");
}
}
TeacherDean 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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900