Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
var oBackgroundGrid = new Grid
           {
               Opacity = .5 ,
               DataContext = PortSettings
           };

           oBackgroundGrid.SetBinding ( Panel.BackgroundProperty ,

           new Binding
           {
               Source = PortSettings,
               Path = new PropertyPath ( "PortSettings.CActual" ) ,
               Converter = new ColorTemplateSelector ( ) ,
               UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged ,
               Mode = BindingMode.TwoWay
           } );


i am creating a grid in code behind but it showing binding error as

System.Windows.Data Error: 40 : BindingExpression path error: 'PortSettings' property not found on 'object' ''CheckboxSetting' (HashCode=63345626)'. BindingExpression:Path=PortSettings.CActual; DataItem='CheckboxSetting' (HashCode=63345626); target element is 'Grid' (Name=''); target property is 'Background' (type 'Brush')


What I have tried:

i try everything, don't know what is the error
Posted
Updated 11-Jan-17 23:04pm
Comments
Jon McKee 12-Jan-17 4:12am    
Where is PortSettings defined? From the error, the binding doesn't seem to be able to locate this property. Would help to see the relationship between PortSettings and where this code is defined.
Mahesh Alappuzha 12-Jan-17 4:18am    
private object _portSettings;
public object PortSettings
{
get { return _portSettings; }
set
{
_portSettings = value;
OnPropertyChanged ( );

}
}
this is the Portsettings Property

PortSettings = card.SettingsList.Where (
setting =>
setting.Ccode.Trim ( ).Equals ( sysSnmp ) ).Select ( setting => setting ).FirstOrDefault ( );
here i assign the the value.
Jon McKee 12-Jan-17 4:33am    
I mean more what object they're defined on. Is the PortSettings property on the same object that oBackgroundGrid is defined on? The definitions look fine - it's probably an issue of where they're defined. WPF can be a hard thing to debug without the full code because so many things can go wrong or get overlooked.
Mahesh Alappuzha 12-Jan-17 4:39am    

namespace Sample
{
public class SampleViewModel: PropertyChangedBase
{

#region [ Public Properties ]




private object _portSettings;
public object PortSettings
{
get { return _portSettings; }
set
{
_portSettings = value;
OnPropertyChanged ( );

}
}

#endregion



#region [ Public Methods ]


private void SampleFunction()
{

GetPortStatus ( );



var oBackgroundGrid = new Grid
{
Opacity = .5 ,
DataContext = PortSettings
};

oBackgroundGrid.SetBinding ( Panel.BackgroundProperty ,

new Binding
{
Source = PortSettings,
Path = new PropertyPath ( "PortSettings.CActual" ) ,
Converter = new ColorTemplateSelector ( ) ,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged ,
Mode = BindingMode.TwoWay
} );

oMasterGrid.Children.Add ( oBackgroundGrid );




public void GetPortStatus ( )
{

PortSettings = card.SettingsList.Where (
setting =>
setting.Ccode.Trim ( ).Equals ( sysSnmp ) ).Select ( setting => setting ).FirstOrDefault ( );
}

#endregion


}
}
Afzaal Ahmad Zeeshan 12-Jan-17 4:52am    
Your code needs a lot of type casting...

1 solution

Took me a second but I think I know what's going on here. The Source should be set to the object (or parent) which contains the property you wish to access while the Path is the path to that property from the source. Try
C#
new Binding
{
Source = this,
Path = new PropertyPath ( "PortSettings" ) ,
Converter = new ColorTemplateSelector ( ) ,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged ,
Mode = BindingMode.TwoWay
}


EDIT: Note: If you want to access a property of PortSettings like CActual you need to cast the property or set the type of property to the proper type instead of object. Then just change the Path to new PropertyPath("PortSettings.CActual").
 
Share this answer
 
v2
Comments
Mahesh Alappuzha 12-Jan-17 8:41am    
its working....
Thank you so much Jon McKee

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