Binding Error Reported with Converter in WPF Textbox - wpf

a textbox is either hidden or not depending on whether its text is null or not.
The actual hiding works ok but i keep getting a Data.Error as follows.
System.Windows.Data Error: 40 : BindingExpression path error: 'new_file_path' property not found on 'object' ''main_window_vm' (HashCode=44962972)'. BindingExpression:Path=new_file_path; DataItem='main_window_vm' (HashCode=44962972); target element is 'Run' (HashCode=28141317); target property is 'Text' (type 'String')
xaml is
<Padding="10" Visibility="{Binding Path=Text, RelativeSource={RelativeSource Self},
Converter={StaticResource null_to_viz}}">
<Run Text="Updated file path : " />
<Run Text="{Binding new_file_path}" />
any ideas to fix this error?

The issue is complaining about not finding new_file_path yet the example is binding to the property Text.
Most likely this converter is not failing and it is a different control. Investigate by either finding the right control which is not binding properly or verifying that textbox is actually binding to the proper VM.

seems like this error occurs when one foolishly set a property to private, not public. Which is why it was not able to be found.

Related

WPF binding at runtime not working

Using .NET 4.7.1 WPF with MahApps 1.6.1.4. I'm binding the HamburgerMenuIconItem.Icon like this:
<controls:HamburgerMenuIconItem.Icon>
<Ellipse">
<Ellipse.Fill>
<ImageBrush ImageSource="{Binding Image}" />
</Ellipse.Fill>
</Ellipse>
</controls:HamburgerMenuIconItem.Icon>
In Design mode I can right click "Image" and go to the property in my ViewModel that is the UserControl.DataContext. As expected.
But in runtime this fails with this error:
System.Windows.Data Error: 40 : BindingExpression path error: 'Image' property not found on 'object' ''HamburgerMenuIconItem' (HashCode=44346036)'. BindingExpression:Path=Image; DataItem='HamburgerMenuIconItem' (HashCode=44346036); target element is 'ImageBrush' (HashCode=21345065); target property is 'ImageSource' (type 'ImageSource')
So at runtime it is trying to bind to a property in the "HamburgerMenuIconItem" or what? I can force the binding to the View code behind like this:
{Binding Path=Image, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}
Which works at runtime also, but I'd like the property in my ViewModel.
What am I misunderstanding?
Seems the issue is missing inheritance of the DataContext (but only at Runtime). The solution explained here solved my issue.

textbox is being filled but datepicker is not via mvvm binding

I have a textbox with a date in it and I want to convert it to a datepicker for twoway binding. But unfortunately the datepicker doesn't show the date.
<TextBox Grid.Row="1"
Grid.Column="4"
IsReadOnly="False"
Text="{Binding Path=BirthDate,
Mode=OneWay,
StringFormat='dd/MM/yyyy'}" />
The following code does not work, the selecteddate shows empty... (while there is a value in it)
<DatePicker Grid.Row="1"
Grid.Column="4" FirstDayOfWeek="Monday" SelectedDate="{Binding BirthDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ></DatePicker>
In another location in the application I also have a datepicker that Does work:
<DatePicker x:Name="DatePicker"
Grid.Row="3"
Grid.Column="1"
FirstDayOfWeek="Monday"
SelectedDate="{Binding Parameter.PAR_Date,
Mode=TwoWay}" />
I can't see what I'm doing wrong, it seems very simple.
I can just type a date in the datepicker and this works... so very strange... any ideas would be welcome...
EDIT:
Got a step closer by using snoop and looking in to the binding on the datepicker and it seems to have an error, I seem to have a different expected type:
System.Windows.Data Error: 1 : Cannot create default converter to perform 'two-way' conversions between types 'EVD.Framework.NullableDateTime' and 'System.Nullable`1[System.DateTime]'. Consider using Converter property of Binding. BindingExpression:Path=Birthdate; DataItem='SomeModel' (HashCode=31671132); target element is 'DatePicker' (Name=''); target property is 'SelectedDate' (type 'Nullable`1')
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='04/10/1929' BindingExpression:Path=Birthdate; DataItem='SomeModel' (HashCode=31671132); target element is 'DatePicker' (Name=''); target property is 'SelectedDate' (type 'Nullable`1')
EDIT 2:
That was it, in the code the datetime was a NullableDateTime. Thanks to everyone pitching in ideas, especially Viv. Votes up for everyone with a decent suggestion :-)
This article does a good job explaining StringFormat:
http://elegantcode.com/2009/04/07/wpf-stringformat-in-xaml-with-the-stringformat-attribute/
You need to remember to use {0} when formatting.

WPF Element Binding

I have two controls within my UserControl where I bind to the exact same object using Element Binding:
AllowNext="{Binding ElementName=MainGrid, Path=DataContext.CanContinue}"
On the first control it works fine but on the second I get a binding exception:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=MainGrid'. BindingExpression:Path=DataContext.CanContinue; DataItem=null; target element is 'WizardPage' (Name='DeductionPage'); target property is 'AllowNext' (type 'Boolean')
I have also tried using RelativeSource binding on the second control:
AllowNext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}, Path=DataContext.CanContinue}"
But this also gives me an error.
Does anyone know what this might be?
--
Here is the simplified control:
<Grid Name="MainGrid">
<w:Wizard Name="MyWizard" w:Designer.PageIndex="1" DataContext="{Binding ElementName=MainGrid, Path=DataContext.Policy}" >
<w:WizardPage Header="Main Member" MaxHeight="600" AllowNext="{Binding ElementName=MainGrid, Path=DataContext.CanContinue}" Name="MainPage">
</w:WizardPage>
<w:WizardPage Name="DeductionPage" Header="Policy Details" AllowBack="False" AllowNext="{Binding ElementName=MainGrid, Path=DataContext.CanContinue}">
</w:WizardPage>
</w:Wizard>
</Grid>
Now as I mentioned, MainPage binds fine, whereas the DeductionPage does not bind at all and gets the supplied error. The DataContext of MainGrid is set from code behind:
public void SetDataContext(object o)
{
MainGrid.DataContext = o;
}
I bet it's the MainGrid which is the binding source is not in the logical tree of your binding target.
This is the problem absolutely with the binding element. However, you didn't give the source so You want to debug it and solve the issue.
Refer the below url and "Cannot find source for binding with reference" section where explained obviously how to debug and solve it.
http://www.codeproject.com/Articles/244107/Debugging-WPF-data-bindings

Binding to dependency property via PlacementTarget fails - wrong path syntax?

No problem
I created a dependency property which takes a string value. I set it on a TextBlock and it works:
<TextBlock dp:ElementDataContext.ElementName="LvMain">
I verified that the property ElementDataContext.ElementName is set to "LvMain".
Problem
Now here is the problem: in the TextBlock's context menu I want to bind to this dependency property via PlacementTarget.
Here is how I try to do it. This is an excerpt of my XAML containing TextBlock and ContextMenu:
<TextBlock dp:ElementDataContext.ElementName="LvMain">
<TextBlock.ContextMenu>
<ContextMenu Tag="{Binding PlacementTarget.(dp:ElementDataContext.ElementName), RelativeSource={RelativeSource Self}}">
This fails at runtime. When opening the context menu it gives me a "BindingExpression path error":
BindingExpression path error: '(dp:ElementDataContext.ElementName)' property not found on 'object' ''TextBlock' (Name='')'. BindingExpression:Path=PlacementTarget.(dp:ElementDataContext.ElementName); DataItem='ContextMenu' (Name='contextMenu'); target element is 'ContextMenu' (Name='contextMenu'); target property is 'Tag' (type 'Object')
I suspect my binding path is wrong. I tried
PlacementTarget.(dp:ElementDataContext.ElementName)
PlacementTarget.dp:ElementDataContext.ElementName
PlacementTarget.ElementDataContext.ElementName
Nothing works. What is the correct syntax? Is this even possible?
The property path syntax PlacementTarget.(dp:ElementDataContext.ElementName) is correct, but you also have to explicitly write the Path=... part in the property expression:
<ContextMenu Tag="{Binding Path=PlacementTarget.(dp:ElementDataContext.ElementName),
RelativeSource={RelativeSource Self}}">
However, the Implicit Path section in Binding Markup Extension does not mention this behaviour.

Binding to the Current Item (WPF)

I am attempting to bind a ListView control to a DataTable, but the WPF binding system seems to be complaining about the binding path I specify.
As an example, a GridViewColumn is defined as follows:
<GridViewColumn Header="ColumnTitle"
DisplayMemberBinding="{Binding Path=/,
Converter={StaticResource myConverter}}"/>
As far as I understand (and MSN seems to support me), specifying Path=/ should make the binding on the current item of the data collection.
The error I receive (in the trace window) is:
System.Windows.Data Error: 39 : BindingExpression path error: ''
property not found on 'current item of collection' ''OrdersRow'
(HashCode=680171)'. BindingExpression:Path=/; DataItem='OrdersRow'
(HashCode=680171); target element is 'TextBlock' (Name=''); target
property is 'Text' (type 'String')
This is giving me the impression that / isn't even a valid path, and WPF is expecting something after the slash. If so, how else would I bind to the current item? Why am I getting this error in the first place?
Have you tried omitting the Path parameter?
<GridViewColumn Header="ColumnTitle"
DisplayMemberBinding="{Binding Converter={StaticResource myConverter}}"/>
I think the confusion is that the DataContext for the GridViewColumn is not the top collection, but is already the item that is bound to that column, so you don't need to specify a path.
The time that the you may use a path like this is if your control's DataContext is a List and you want to bind to the selected item. A possible example would be.
<Combobox DataContext={Binding ColourList}
DataSource={Binding} <!--Bind to the datacontext -->
ForeColor={Binding/} <!--Bind to the currently selected item
in the datacontext -->
/>

Resources