简体   繁体   中英

WPF OneWay binding works only once

I have a View that have two comboboxes. One is where user selects routing pipe type name, and the other where there should be a list of available diameters for the chosen pipe type.

Whenever user selects the pipe type, the other combobox should update the list of available diameters.

AvailableDiameters and RoutingPipeTypeName properties are static in Context class, that implements INotifyPropertyChanged interface. In xaml I've set the bindings to these properties, in code behind also the DataContext.

The problem is that the list of diameters get's updated only once, when the view is initialized.

When debugging I can see that the properties backing field's values are updated properly when selection on pipe type name is changed, only in the UI the available diameters list is not updated...

Context class:

public class Context : INotifyPropertyChanged
{
    public static Context This { get; set; } = new Context();
    public static string RoutingPipeTypeName
    {
        get => _routingPipeTypeName;
        set
        {
            if (_routingPipeTypeName != value)
            {
                _routingPipeTypeName = value;

               This.OnPropertyChanged(nameof(RoutingPipeTypeName));
            }
        }
    }

    public static List<double> AvailableDiameters
    {
        get => _availableDiameters;
        set
        {
            //check if new list's elements are not equal
            if (!value.All(_availableDiameters.Contains))
            {
                _availableDiameters = value;
                This.OnPropertyChanged(nameof(AvailableDiameters));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

xaml:

<ComboBox Width="80" SelectedValue="{Binding Path=RoutingPipeTypeName, Mode=OneWayToSource}">
                    <ComboBoxItem Content="Example pipe type 1"></ComboBoxItem>
                    <ComboBoxItem Content="Example pipe type 2"></ComboBoxItem>
</ComboBox>

<ComboBox Width="80" SelectedValue="{Binding Path=RoutingDiameter, Mode=OneWayToSource}" ItemsSource="{Binding Path=AvailableDiameters, Mode=OneWay}">
                </ComboBox>

code behind:

public Context AppContext => Context.This;

public MyView()
{
    InitializeComponent();

    Instance = this;
    DataContext = AppContext;
}

And the client class that is responsible for updating the list of diameters:

public void InitializeUIContext()
{
    Context.This.PropertyChanged += UIContextChanged;

    if (Cache.CachedPipeTypes.Count > 0)
        Context.RoutingPipeTypeName = Cache.CachedPipeTypes.First().Key;
}

private void UIContextChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    if (e.PropertyName == nameof(Context.RoutingPipeTypeName))
    {
        Context.AvailableDiameters = Cache.CachedPipeTypes.First().Value.GetAvailableDiameters();
    }
}

I expected such set-up would update the diameters combobox each time the selection is changed on the pipe types property. Instead it updates it only once, when the view is initialized... Why?

Do not use static properties for binding to an object (which you have correctly passed to the DataContext of your view).

Declare the properties without the static modifier and replace This.OnPropertyChanged by OnPropertyChanged :

public string RoutingPipeTypeName
{
    get => _routingPipeTypeName;
    set
    {
        if (_routingPipeTypeName != value)
        {
            _routingPipeTypeName = value;
            OnPropertyChanged(nameof(RoutingPipeTypeName));
        }
    }
}

You should also remove the static This from your Context class and simply write

public Context AppContext { get; } = new Context();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM