Jeremiah Mercier
Jeremiah Mercier

Reputation: 501

How do I use x:Bind function binding with a null parameter?

I am trying to format several properties of an object and bind the result to a TextBlock using x:Bind function binding. The binding looks like this:

<TextBlock Text="{x:Bind local:MainViewModel.FormatWidget(ViewModel.SelectedItem), Mode=OneWay}" />

As long as the object is not null, this works perfectly. However, when the object is null, my function is not called. Or to be more precise, if the object is null initially, the function is called, but if the object changes to null later, the function is not called.

Why is the function not being called when the parameter is null and how can I use it for this case?

Here's a repro. When you run it, notice that initially the function binds correctly to the null SelectedItem and displays "No widget selected." But when you select an item and then unselect it (CTRL + click to unselect), it does not call the function and displays the FallbackValue. (If the FallbackValue is not set, it does not update the binding at all.)

MainPage.xaml

<Page
    x:Class="NullFunctionBindingParameter.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:NullFunctionBindingParameter">
    <Page.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Margin" Value="20" />
        </Style>
    </Page.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <ListView
            Grid.Column="0"
            ItemsSource="{x:Bind ViewModel.Widgets, Mode=OneWay}"
            SelectedItem="{x:Bind ViewModel.SelectedItem, Mode=TwoWay}">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="local:Widget">
                    <TextBlock Text="{x:Bind Name}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <TextBlock Grid.Column="1" Text="{x:Bind local:MainViewModel.FormatWidget(ViewModel.SelectedItem), Mode=OneWay, FallbackValue=MyFallbackValue}" />
    </Grid>
</Page>

MainPage.xaml.cs

using Windows.UI.Xaml.Controls;

namespace NullFunctionBindingParameter
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            InitializeComponent();
        }

        public MainViewModel ViewModel { get; } = new MainViewModel();
    }
}

MainViewModel.cs

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace NullFunctionBindingParameter
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private Widget _selectedItem;

        public Widget SelectedItem
        {
            get => _selectedItem;
            set
            {
                if (_selectedItem != value)
                {
                    _selectedItem = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedItem)));
                }
            }
        }

        public ObservableCollection<Widget> Widgets { get; } = new ObservableCollection<Widget>()
        {
            new Widget
            {
                Id = Guid.NewGuid(),
                Name = "Regular Widget",
                Model = "WX2020-01",
                Description = "Your typical everyday widget."
            },
            new Widget
            {
                Id = Guid.NewGuid(),
                Name = "Super Widget",
                Model = "WX2020-02",
                Description = "An extra special upgraded widget."
            },
            new Widget
            {
                Id = Guid.NewGuid(),
                Name = "Broken Widget",
                Model = "WX2020-03",
                Description = "A widget that has been used and abused."
            },
            new Widget
            {
                Id = Guid.NewGuid(),
                Name = "Fake Widget",
                Model = "WX2020-04",
                Description = "It's not really a widget at all!"
            },
            new Widget
            {
                Id = Guid.NewGuid(),
                Name = "Surprise Widget",
                Model = "WX2020-05",
                Description = "What kind of widget will it be?"
            },
            new Widget
            {
                Id = Guid.NewGuid(),
                Name = "Invisible Widget",
                Model = "WX2020-06",
                Description = "Our most inexpensive widget."
            },
            new Widget
            {
                Id = Guid.NewGuid(),
                Name = "Backwards Widget",
                Model = "WX2020-07",
                Description = "Really more of a tegdiw, come to think of it."
            }
        };

        public static string FormatWidget(Widget widget)
        {
            if (widget == null)
                return "No widget selected";
            else
                return $"{widget.Name} [{widget.Model}] {widget.Description}";
        }

        public string GetFormattedWidget()
        {
            return FormatWidget(SelectedItem);
        }
    }
}

Widget.cs

using System;
using System.ComponentModel;

namespace NullFunctionBindingParameter
{
    public class Widget : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private Guid _id;
        private string _name;
        private string _model;
        private string _description;

        public Guid Id
        {
            get => _id;
            set
            {
                if (_id != value)
                {
                    _id = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Id)));
                }
            }
        }

        public string Name
        {
            get => _name;
            set
            {
                if (_name != value)
                {
                    _name = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Name)));
                }
            }
        }

        public string Model
        {
            get => _model;
            set
            {
                if (_model != value)
                {
                    _model = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Model)));
                }
            }
        }

        public string Description
        {
            get => _description;
            set
            {
                if (_description != value)
                {
                    _description = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Description)));
                }
            }
        }
    }
}

Upvotes: 1

Views: 967

Answers (1)

Anran Zhang
Anran Zhang

Reputation: 7727

In this case, I recommend that you use Converter instead of using static methods directly in the binding statement.

WidgetConverter

public class WidgetConverter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var widget = value as Widget;
        return MainViewModel.FormatWidget(widget);
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

Usage

<Page.Resources>
    ...
    <local:WidgetConverter x:Key="WidgetConverter"/>
</Page.Resources>

...

<TextBlock
    Grid.Row="2"
    Grid.Column="2"
    Text="{x:Bind ViewModel.SelectedItem, Mode=OneWay,Converter={StaticResource WidgetConverter}}"/>

Best regards.

Upvotes: 1

Related Questions