Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. Binding to XAML initialized straight C# class

Binding to XAML initialized straight C# class

Scheduled Pinned Locked Moved WCF and WF
wpfcsharpdotnetwcfcom
6 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RNEELY
    wrote on last edited by
    #1

    The text boxes come up blank in the following app and I would like them to show the name and number of my favorite peep. Any ideas? namespace Dependent { public class Peep { public Peep() { } private string name; public string Name { get { return name;} set { name = value;} } private Int32 number; public Int32 Number { get { return number;} set { number = value;} } } } <Window x:Class="Dependent.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Dependent" > <Window.Resources> <local:Peep x:Key="MyBestPeep" Name="Lisa" Number="5551212" /> </Window.Resources> <StackPanel> <TextBox x:Name="txtName" Text="{Binding Source=MyBestPeep, Path=Name, Mode=Default}"/> <TextBox x:Name="txtNumber" Text="{Binding Source=MyBestPeep, Path=Number, Mode=Default}"/> </StackPanel> </Window>

    Sincerely, -Ron

    L 1 Reply Last reply
    0
    • R RNEELY

      The text boxes come up blank in the following app and I would like them to show the name and number of my favorite peep. Any ideas? namespace Dependent { public class Peep { public Peep() { } private string name; public string Name { get { return name;} set { name = value;} } private Int32 number; public Int32 Number { get { return number;} set { number = value;} } } } <Window x:Class="Dependent.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Dependent" > <Window.Resources> <local:Peep x:Key="MyBestPeep" Name="Lisa" Number="5551212" /> </Window.Resources> <StackPanel> <TextBox x:Name="txtName" Text="{Binding Source=MyBestPeep, Path=Name, Mode=Default}"/> <TextBox x:Name="txtNumber" Text="{Binding Source=MyBestPeep, Path=Number, Mode=Default}"/> </StackPanel> </Window>

      Sincerely, -Ron

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Have Peep implement the INotifyPropertyChanged interface.

      Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP Profile

      Just a grain of sand on the worlds beaches.

      R 1 Reply Last reply
      0
      • L Lost User

        Have Peep implement the INotifyPropertyChanged interface.

        Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP Profile

        Just a grain of sand on the worlds beaches.

        R Offline
        R Offline
        RNEELY
        wrote on last edited by
        #3

        Thanks Karl. I tried the following to no avail... namespace Dependent { public class Peep : INotifyPropertyChanged { public Peep() { } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } private string name; public string Name { get { return name; } set { name = value; NotifyPropertyChanged("Name"); } } private Int32 number; public Int32 Number { get { return number; } set { number = value; NotifyPropertyChanged("Number"); } } } } The following appears in the output window on run: System.Windows.Data Error: 35 : BindingExpression path error: 'Name' property not found on 'object' ''String' (HashCode=-1333902949)'. BindingExpression:Path=Name; DataItem='String' (HashCode=-1333902949); target element is 'TextBox' (Name='txtName'); target property is 'Text' (type 'String') System.Windows.Data Error: 35 : BindingExpression path error: 'Number' property not found on 'object' ''String' (HashCode=-1333902949)'. BindingExpression:Path=Number; DataItem='String' (HashCode=-1333902949); target element is 'TextBox' (Name='txtNumber'); target property is 'Text' (type 'String')

        Sincerely, -Ron

        L 1 Reply Last reply
        0
        • R RNEELY

          Thanks Karl. I tried the following to no avail... namespace Dependent { public class Peep : INotifyPropertyChanged { public Peep() { } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } private string name; public string Name { get { return name; } set { name = value; NotifyPropertyChanged("Name"); } } private Int32 number; public Int32 Number { get { return number; } set { number = value; NotifyPropertyChanged("Number"); } } } } The following appears in the output window on run: System.Windows.Data Error: 35 : BindingExpression path error: 'Name' property not found on 'object' ''String' (HashCode=-1333902949)'. BindingExpression:Path=Name; DataItem='String' (HashCode=-1333902949); target element is 'TextBox' (Name='txtName'); target property is 'Text' (type 'String') System.Windows.Data Error: 35 : BindingExpression path error: 'Number' property not found on 'object' ''String' (HashCode=-1333902949)'. BindingExpression:Path=Number; DataItem='String' (HashCode=-1333902949); target element is 'TextBox' (Name='txtNumber'); target property is 'Text' (type 'String')

          Sincerely, -Ron

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          After stepping throught the code, the Peep class is never being instantiated. Not sure why. I altered the solution to use a DataContent and it works fine. Imports System.ComponentModel Public Class Peep Implements INotifyPropertyChanged Private _strName As String = String.Empty Private _strNumber As String = String.Empty Public Event PropertyChanged(ByVal sender As Object, _ ByVal e As System.ComponentModel.PropertyChangedEventArgs) _ Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged Public Property Name() As String Get Return _strName End Get Set(ByVal Value As String) _strName = Value OnPropertyChanged("Name") End Set End Property Public Property Number() As String Get Return _strNumber End Get Set(ByVal Value As String) _strNumber = Value OnPropertyChanged("Number") End Set End Property Private Sub OnPropertyChanged(ByVal strPropertyName As String) If PropertyChangedEvent IsNot Nothing Then RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(strPropertyName)) End If End Sub Public Sub New() End Sub End Class Class Window1 Private Sub Window1_Loaded(ByVal sender As Object, _ ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded Dim objPeep As New Peep objPeep.Name = "Hello World" objPeep.Number = "411" Me.DataContext = objPeep End Sub End Class

          <Window
          x:Class="Window1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:local="clr-namespace:Peeper"
          Title="Window1" Height="300" Width="300">
          <StackPanel>
          <TextBox x:Name="txtName" Text="{Binding Path=Name, Mode=Default}" />
          <TextBox x:Name="txtNumber" Text="{Binding Path=Number, Mode=Default}" />
          </StackPanel>
          </Window>

          Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page |

          R 1 Reply Last reply
          0
          • L Lost User

            After stepping throught the code, the Peep class is never being instantiated. Not sure why. I altered the solution to use a DataContent and it works fine. Imports System.ComponentModel Public Class Peep Implements INotifyPropertyChanged Private _strName As String = String.Empty Private _strNumber As String = String.Empty Public Event PropertyChanged(ByVal sender As Object, _ ByVal e As System.ComponentModel.PropertyChangedEventArgs) _ Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged Public Property Name() As String Get Return _strName End Get Set(ByVal Value As String) _strName = Value OnPropertyChanged("Name") End Set End Property Public Property Number() As String Get Return _strNumber End Get Set(ByVal Value As String) _strNumber = Value OnPropertyChanged("Number") End Set End Property Private Sub OnPropertyChanged(ByVal strPropertyName As String) If PropertyChangedEvent IsNot Nothing Then RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(strPropertyName)) End If End Sub Public Sub New() End Sub End Class Class Window1 Private Sub Window1_Loaded(ByVal sender As Object, _ ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded Dim objPeep As New Peep objPeep.Name = "Hello World" objPeep.Number = "411" Me.DataContext = objPeep End Sub End Class

            <Window
            x:Class="Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:Peeper"
            Title="Window1" Height="300" Width="300">
            <StackPanel>
            <TextBox x:Name="txtName" Text="{Binding Path=Name, Mode=Default}" />
            <TextBox x:Name="txtNumber" Text="{Binding Path=Number, Mode=Default}" />
            </StackPanel>
            </Window>

            Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page |

            R Offline
            R Offline
            RNEELY
            wrote on last edited by
            #5

            Turns out I forgot to add StaticResource to the original xaml. Following works: <Window x:Class="Dependent.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Dependent" > <Window.Resources> <local:Peep x:Key="MyBestPeep" Name="Lisa" Number="5551212" /> </Window.Resources> <StackPanel> <TextBox Text="{Binding Source={StaticResource MyBestPeep}, Path=Name, Mode=Default}" /> <TextBox Text="{Binding Source={StaticResource MyBestPeep}, Path=Number, Mode=Default}"/> <Button Click="Button_Click">NewBestPeep</Button> </StackPanel> </Window> using System; using System.Windows; using System.ComponentModel; namespace Dependent { public class Peep : INotifyPropertyChanged { public Peep() {} public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } private string name; public string Name { get { return name; } set { name = value; NotifyPropertyChanged("Name");} } private Int32 number; public Int32 Number { get { return number; } set { number = value; NotifyPropertyChanged("Number");} } } public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { Peep mybestpeep = TryFindResource("MyBestPeep") as Peep; mybestpeep.Name = "Denis"; mybestpeep.Number = 5551234; mybestpeep.Name = "Bragi"; mybestpeep.Number = 5554321; } } }

            Sincerely, -Ron

            L 1 Reply Last reply
            0
            • R RNEELY

              Turns out I forgot to add StaticResource to the original xaml. Following works: <Window x:Class="Dependent.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Dependent" > <Window.Resources> <local:Peep x:Key="MyBestPeep" Name="Lisa" Number="5551212" /> </Window.Resources> <StackPanel> <TextBox Text="{Binding Source={StaticResource MyBestPeep}, Path=Name, Mode=Default}" /> <TextBox Text="{Binding Source={StaticResource MyBestPeep}, Path=Number, Mode=Default}"/> <Button Click="Button_Click">NewBestPeep</Button> </StackPanel> </Window> using System; using System.Windows; using System.ComponentModel; namespace Dependent { public class Peep : INotifyPropertyChanged { public Peep() {} public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } private string name; public string Name { get { return name; } set { name = value; NotifyPropertyChanged("Name");} } private Int32 number; public Int32 Number { get { return number; } set { number = value; NotifyPropertyChanged("Number");} } } public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { Peep mybestpeep = TryFindResource("MyBestPeep") as Peep; mybestpeep.Name = "Denis"; mybestpeep.Number = 5551234; mybestpeep.Name = "Bragi"; mybestpeep.Number = 5554321; } } }

              Sincerely, -Ron

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              My Bad. I should have noticed this. Good job!

              Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP Profile

              Just a grain of sand on the worlds beaches.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups