chrisjbampton
Topic Author
Posts: 1
Joined: 03 Apr 2018, 13:49

BindingExpressionBase

08 Dec 2019, 18:03

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
 
User avatar
sfernandez
Site Admin
Posts: 2262
Joined: 22 Dec 2011, 19:20

Re: BindingExpressionBase

11 Dec 2019, 17:14

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.
 
andreasg
Posts: 10
Joined: 22 Nov 2013, 08:44

Re: BindingExpressionBase

13 Apr 2021, 11:35

How to perform this in Noesis 3?
 
User avatar
sfernandez
Site Admin
Posts: 2262
Joined: 22 Dec 2011, 19:20

Re: BindingExpressionBase

14 Apr 2021, 10:36

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());
}

Who is online

Users browsing this forum: No registered users and 5 guests