BindingExpression evaluated?
Posted: 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:
Attached property:
During the sort process, I create a new Binding based on the BindingExpression, and get its value:
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?
XAML:
Code: Select all
<GridViewColumn Width="120"
ap:ColumnSort.PropertyName="{Binding Salary}"
DisplayMemberBinding="{Binding Salary, Converter={StaticResource CurrencyConverter}}"
Header="{t:Translate Salary}" />
Code: Select all
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);
}
Code: Select all
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);
}
How can I specify a BindingExpression without Noesis evaluating it?