Binding to XAML initialized straight C# class
-
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
-
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
Have Peep implement the INotifyPropertyChanged interface.
Cheers, Karl
» CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP ProfileJust a grain of sand on the worlds beaches.
-
Have Peep implement the INotifyPropertyChanged interface.
Cheers, Karl
» CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP ProfileJust a grain of sand on the worlds beaches.
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
-
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
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 | -
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 |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
-
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
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 ProfileJust a grain of sand on the worlds beaches.