Page 1 of 1

BindingExpression evaluated?

Posted: 25 Dec 2023, 01:10
by nadjibus
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?

Re: BindingExpression evaluated?

Posted: 02 Jan 2024, 10:59
by sfernandez
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!

Re: BindingExpression evaluated?

Posted: 03 Jan 2024, 15:06
by nadjibus