Click here to Skip to main content
Rate this: bad
good
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"
             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:
 
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 23-Jun-13 21:37pm
Comments
Alexander Dymshyts at 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 at 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 at 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");
}
}
deanmichaelcook at 24-Jun-13 4:19am
   
Ok, I will try this, however putting the RaisePropertyChange throws an error that it does not exist.
Alexander Dymshyts at 24-Jun-13 4:22am
   
You should implement INotifyPropertyChanged interface
deanmichaelcook at 24-Jun-13 4:23am
   
I'm sorry, I have never used that class before, could you provide a sample please?
Alexander Dymshyts at 24-Jun-13 4:25am
   
http://www.c-sharpcorner.com/UploadFile/ptmujeeb/wpf-mvvm-pattern-a-simple-tutorial-for-absolute-beginners/
Alexander Dymshyts at 24-Jun-13 4:30am
   
Or may be this example will help you with your problem - http://www.codeproject.com/Articles/15822/Bind-Better-with-INotifyPropertyChanged
stibee at 24-Jun-13 12:17pm
   
For your buttons I would recommend
http://www.codeproject.com/Questions/563091/How-to-create-multiple-commands-in-a-viewmodel-WPF

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 169
1 Zoltán Zörgő 139
2 George Jonsson 135
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 24 Jun 2013
Copyright © CodeProject, 1999-2014
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