Page 1 of 1

BindingExpressionBase

Posted: 08 Dec 2019, 18:03
by chrisjbampton
Try to work out how to do binding against a ListView column through C# and bumping into some issues...
<ListView x:Name="InventoryItemList" Grid.Row="1" Margin="4" ItemsSource="{Binding ItemsList}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Item" Width="96" DisplayMemberBinding="{Binding Item}" />
                    <GridViewColumn Header="Quantity" Width="64" DisplayMemberBinding="{Binding Quantity}" />
                </GridView>
            </ListView.View>
        </ListView>
This works fine in a XAML file, loading the xaml and data binding objects to it. Great.

I'm trying to work out how to do the same thing in code, to be able to change the columns at runtime.




Inspecting the resulting objects from the XAML above gives the DisplayMemberBinding property of the GridViewColumn.

It's set to a BindingExpressionBase object.

BindingExpressionBase, or BindingExpression don't have useful constructors.

I thought I could use BindingOperations.SetBinding(target, dp) to get a BindingExpressionBase, but GridViewColumn doesn't have a DisplayMemberPathProperty to attach too.

Any ideas?

Thanks

Chris

Re: BindingExpressionBase

Posted: 11 Dec 2019, 17:14
by sfernandez
Hi,

I see that in WPF the DisplayMemberBinding property is of BindingBase type, could you please report that in our bugtracker so we can change it in a future version?

In the meantime, to create the associated expression from a Binding you can call ProvideValue:
void UpdateColumnBinding(GridViewColumn column, string path)
{
  Binding binding = new Binding(path);
  column.DisplayMemberBinding = (BindingExpression)binding.ProvideValue(null, null);
}
Hope this helps.

Re: BindingExpressionBase

Posted: 13 Apr 2021, 11:35
by andreasg
How to perform this in Noesis 3?

Re: BindingExpressionBase

Posted: 14 Apr 2021, 10:36
by sfernandez
In Noesis 3 the Binding expects an object implementing the IProvideValueTarget interface. Something like this:
public struct ProvideValueTarget : IServiceProvider, IProvideValueTarget
{
    public object TargetObject { get; set; }
    public object TargetProperty { get; set; }

    object IServiceProvider.GetService(Type serviceType)
    {
        return serviceType == typeof(IProvideValueTarget) ? (object)this : null;
    }
}
Then you can pass an instance of this object setting the corresponding TargetObject and TargetProperty (null in this case):
void UpdateColumnBinding(GridViewColumn column, string path)
{
  Binding binding = new Binding(path);
  column.DisplayMemberBinding = (BindingExpression)binding.ProvideValue(new ProvideValueTarget());
}

Re: BindingExpressionBase

Posted: 27 Jul 2022, 19:00
by colin14321
Hi there,

I just want clarification, is this going to be left as a BindingExpressionBase in the future, or will this be changed back to BindingBase to match WPF?

Thank you.

Re: BindingExpressionBase

Posted: 28 Jul 2022, 12:08
by sfernandez
Hi colin, we had a pending issue to solve this (#1337). I added it for our next major version roadmap to avoid breaking current code in 3.1.X versions.

Re: BindingExpressionBase

Posted: 28 Jul 2022, 21:26
by colin14321
Thank you !