Binding to the Current Item (WPF) - 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 -->
/>

Related

Binding Error Reported with Converter in WPF Textbox

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.

wpf debug error output System.WIndows.Data Error 25

I have a custom styled Combobox which works fine. It is placed inside a usercontrol and bound to a data structure. I use DisplayMemberPath to show only one element in the Combobox TextBox. The ComboBox Style is taken from MSDN and used many times. So it's not displayed here.
<UserControl x:Class="wpf.projext1.MyComboBox"
x:Name="MyControl"
...
<ComboBox Style="{StaticResource ComboBoxStyle}"
Text="{Binding ElementName=MyControl, Path=Text}"
IsEditable="True"
IsTextSearchEnabled="False"
StaysOpenOnEdit="True"
ItemsSource="{Binding ElementName=MyControl, Path=MyItemsSource}"
DisplayMemberPath="Name"
</ComboBox
I get the following annoying error message populating the output window:
System.Windows.Data Error: 25 : Both 'ContentTemplate' and 'ContentTemplateSelector' are set; 'ContentTemplateSelector' will be ignored. ComboBoxItem:'ComboBoxItem' (Name='')
if i leave out the
DisplayMemberPath="Name"
... no debug output about error 25 is shown. But I definitely need DiplayMemberPath="Name"!
Do You have an idea to fix this ?
You can´t set both DisplayMemberPath and ItemTemplate at the same time.
DisplayMemberPath is used to tell the ItemsControl which property to display when showing your objects. It makes no sense to set this field if you're already passing a custom ItemTemplate, since you can choose how to show the object within that ItemTemplate.
Since the default Combobox style from MSDN also sets an ItemTemplate, this is likely the cause of the error.
resolved: use the TextSearch attached property, no matter if TextSearch is enabled!
TextSearch.TextPath="Name"
Should note TextSearch.TextPath="Name" instead of DisplayMemberPath="Name".

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

Can't bind to View's property

In my view's xaml file, I have this line:
TextBox Text="{Binding MyModel.Text}"
Everytime I ran the program, it gave me this error message:
System.Windows.Data Error: 40 : BindingExpression path error:
'MyModel' property not found on 'object' ''MyModel'
(HashCode=56593137)'. BindingExpression:Path=MyModel.Text;
DataItem='MyModel' (HashCode=56593137); target element is 'TextBox'
(Name=''); target property is 'Text' (type 'String')
I'm sure my spelling is right.
I set my view's DataContext to ViewModel. Could that be a problem?
If your DataContext is set to MyModel you should just have to write:
<TextBox Text="{Binding Text}"/>
Adding the extra MyModel is repetitive and results in looking for MyModel.MyModel.Text.
Just TextBox Text="{Binding Text}"
Since you're view is bound to your view-model (good), then your view-model needs to have a property that your view will bind to:
TextBox Text="{Binding MyViewModelsProperty}"
In your situation, you'll need to set your model's property from your view-model (MyViewModelsProperty setter).
Let me know if you need more info.

Relative binding command with silverlight WP7

I have an error when binding my command to a button in an ItemsControl.
This is my code :
<phone:PhoneApplicationPage.DataContext>
<ViewModel:MyViewModel />
</phone:PhoneApplicationPage.DataContext>
with :
<ItemsControl ItemsSource="{Binding MyList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="Test"
cmd:ButtonBaseExtensions.Command="{Binding MyViewModel.TestCommand}"
cmd:ButtonBaseExtensions.CommandParameter="{Binding}"/>
</ItemsControl.ItemTemplate>
</ItemsControl>
And I get :
System.Windows.Data Error: BindingExpression path error: 'MyViewModel' property not found on '...' '...' (HashCode=77119633). BindingExpression: Path='MyViewModel.ChooseCommand' DataItem='...' (HashCode=77119633); target element is 'System.Windows.Controls.Button' (Name=''); target property is 'Command' (type 'System.Windows.Input.ICommand')..
Of course, I should use an absolute binding or a relative one, but I don't know how to do that.
Thanks in advance for any help
Your Button is within an ItemsControl which is bound to youur MyList property, which I am guessing is a List or some IEnumerable type. The DataContext of each Button will be the item within the MyList that it represents.
You are correct that to bind the buttons to your top-level view model you would need some sort of relative source binding, which Silverlight (3) does not support.
I created a relative source binding replacement for Silverlight here:
http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/
However, for WP7, where performance really matters, I would not use it!
Why not simply create the relationship you need in your view model? i.e. for each item in MyList (let's call them MyListItem), expose a property which points back to the parent view Model. In other Words, have a MyListItem.Parent property which points to MyViewModel.

Resources