User avatar
nadjibus
Topic Author
Posts: 32
Joined: 24 Feb 2022, 04:09

BindingExpression evaluated?

25 Dec 2023, 01:10

Hello. I'm implementing click to sort on ListView/ViewGrid headers. For that I created a GridViewColumn attached property of type BindingExpression that specifies the sort property:

XAML:
<GridViewColumn Width="120"
                ap:ColumnSort.PropertyName="{Binding Salary}"
                DisplayMemberBinding="{Binding Salary, Converter={StaticResource CurrencyConverter}}"
                Header="{t:Translate Salary}" />
Attached property:
public static class ColumnSort
{
    public static readonly DependencyProperty PropertyNameProperty = DependencyProperty.Register("PropertyName", typeof(BindingExpression), typeof(ColumnSort));

    public static BindingExpression GetPropertyName(DependencyObject target) => (BindingExpression)target.GetValue(PropertyNameProperty);

    public static void SetPropertyName(DependencyObject target, BindingExpression value) => target.SetValue(PropertyNameProperty, value);
}
During the sort process, I create a new Binding based on the BindingExpression, and get its value:
public int Compare(object x, object y)
{
    var xValue = GetPropertyValue(x);
    var yValue = GetPropertyValue(y);

    if (_sortOrder == SortOrder.Ascending)
        return Comparer.Default.Compare(xValue, yValue);

    return Comparer.Default.Compare(yValue, xValue);
}

private object GetPropertyValue(object item)
{
    if (item == null)
        return null;

    var dependencyObject = new UIElement();

    var binding = CreateBinding(item, _bindingExpression); // Create a new Binding from the BindingExpression

    BindingOperations.SetBinding(dependencyObject , DummyProperty, binding);

    return _dependencyObject.GetValue(DummyProperty);
}
In WPF, it works perfectly. But in Noesis, the BindingExpression is evaluated like a normal binding (getting binding errors against the ViewModel as soon as the ListView is loaded) and the attached property returns null. I always thought of BindingExpression as a lazy Binding that does not get evaluated until needed, and it seems that it acts like this in WPF since it works.

How can I specify a BindingExpression without Noesis evaluating it?
 
User avatar
sfernandez
Site Admin
Posts: 2997
Joined: 22 Dec 2011, 19:20

Re: BindingExpression evaluated?

02 Jan 2024, 10:59

This behavior in Noesis is clearly a bug, the BindingExpression should be stored in the property without being evaluated in that case.
Could you please report it in our bugtracker?
Thanks!
 
User avatar
nadjibus
Topic Author
Posts: 32
Joined: 24 Feb 2022, 04:09

Re: BindingExpression evaluated?

03 Jan 2024, 15:06

Who is online

Users browsing this forum: Google [Bot] and 4 guests