0

I have WPF combo based dropdown list that can have 4 items to the max.Out of these there is a last item named "Other"(hard-coded) which allows user to select a new resource.When user selects the new resource it is updated in the dropdown list(still 4 items) and accordingly based on the selected resource, our items in the UI application are updated.It also shows the selected item in UI. The problem am facing can be described as:

Suppose i have the following resources in the drop-down list:

Item 1
Item 2
Item 3
Other

Now when i select 'Other', a dialog is shown to select the resource. From this dialog, i again select 'Item1'(First item in the dropdown list).Now in UI, it still shows the selected item as 'Other'. I investigated and found the root cause as: In the XAML, the dropdown control's item source is bounded to an observable collection named "ResourceList". Whenever a new resource is selected in UI it gets added to this collection and whenever i make a "CHANGE" to this collection only then it is reflected in UI otherwise NOT.

XAML Code:

<inputToolkit:UxDropDown x:Name="cmbResourceList"
                         MaxWidth="150"
                         Margin="8 0"
                         HorizontalAlignment="Left"
                         AutomationProperties.AutomationId="cmbResourceListForTask"
                         Cursor="Hand"
                         FontSize="{StaticResource TaskListHeaderFontSize}"
                         FontWeight="Bold"
                         ItemsSource="{Binding ResourceList}"
                         SelectedValue="{Binding Path=SelectedResource,
                                                Mode=TwoWay,
                                                UpdateSourceTrigger=PropertyChanged}"


                         ToolTip="{Binding Path=SelectedResource.DisplayName,
                                           Mode=OneWay}"
                         Visibility="{Binding Path=ShowResourceList,
                                              Converter={StaticResource boolToVisibility}}">
    <inputToolkit:UxDropDown.ItemTemplate>
        <DataTemplate>
            <TextBlock AutomationProperties.AutomationId="SelectedResourceItemForTask"
                       Cursor="Hand"
                       Text="{Binding DisplayName}"
                       TextTrimming="CharacterEllipsis" />
        </DataTemplate>
    </inputToolkit:UxDropDown.ItemTemplate>
</inputToolkit:UxDropDown>

In this binding 'SelectedValue' is binded to 'SelectedResource' property in view model. In the property 'SelectedResource' we are firing property changed notification whenever it assumes a new value. Still not reflected in UI because of the root cause i just mentioned above. Any solution to this problem?

Filburt
  • 17,626
  • 12
  • 64
  • 115
sandy
  • 616
  • 2
  • 9
  • 20

1 Answers1

0

Lets suspect that you made something like this:

private string _Selection;
    public string Selection
    {
        get { return _Selection; }

        set
        {
            if (_Selection != value)
            {
                if (value == "Other")
                {
                    _Selection = ShowOtherDialog();
                }
                else
                {
                    _Selection = value;
                }

                NotifyPropertyChanged("Selection");
            }
        }
    }

Unfortunately, the UI won't update like that since it doesnt listen to notifications while writing the binding back to source.

You might want to try this:

private string _Selection;
    public string Selection
    {
        get { return _Selection; }

        set
        {
            if (_Selection != value)
            {
                if (value == "Other")
                {
                    Application.Current.Dispatcher.BeginInvoke(
                        new Action(() =>
                        {
                            _Selection = ShowOtherDialog();
                            NotifyPropertyChanged("Selection");

                       }));                      
                }
                else
                {
                    _Selection = value;
                    NotifyPropertyChanged("Selection");   
                }


            }
        }
    }

You can read this article for a better explanation.

Community
  • 1
  • 1
SnowballTwo
  • 529
  • 2
  • 11