Dishant
Dishant

Reputation: 1595

ComboBox SelectedItem not getting set when using custom ItemTemplate

I have a ComboBox with custom ItemTemplate, it has a CheckBox which is binding to a ViewModel of that page.

What I am trying to do is to set ComboBox item selected to the first checked value whenever ComboBox is closed, but the value is not getting set accordingly.

Is there anything that I am missing?

Xaml:

<ComboBox x:Name="myComboBox" ItemsSource="{Binding ComboBoxList}" SelectedItem="{Binding SelectedPerson, Mode=TwoWay}" HorizontalAlignment="Center" VerticalAlignment="Center" Height="50" Width="150">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding Path=IsChecked, Mode=TwoWay}" Content="{Binding}" Margin="0" />
                </DataTemplate>
            </ComboBox.ItemTemplate>
            <interactivity:Interaction.Behaviors>
                <core:DataTriggerBehavior Binding="{Binding IsDropDownOpen, ElementName=myComboBox}" ComparisonCondition="NotEqual" Value="True">
                    <core:InvokeCommandAction Command="{Binding DropDownClosedCommand}"/>
                </core:DataTriggerBehavior>
            </interactivity:Interaction.Behaviors>
</ComboBox>

ViewModel:

 public class MainViewModel : INotifyPropertyChanged
{
    private string header;

    public string Header
    {
        get { return header; }
        set
        {
            header = value;
            RaisePropertyChange(nameof(Header));
        }
    }

    private Person selectedPerson;

    public Person SelectedPerson
    {
        get { return selectedPerson; }
        set { selectedPerson = value; RaisePropertyChange(nameof(SelectedPerson)); }
    }


    private ObservableCollection<Person> comboBoxList;

    public ObservableCollection<Person> ComboBoxList
    {
        get { return comboBoxList; }
        set { comboBoxList = value; }
    }


    public DelegateCommand DropDownClosedCommand { get; set; }

    public MainViewModel()
    {
        Header = "My Header";

        ComboBoxList = new ObservableCollection<Person> {
             new Person() { Name = "Person 1", IsChecked = false },
                  new Person() { Name = "Person 2", IsChecked = false },
                  new Person() { Name = "Person 3", IsChecked = false },
                  new Person() { Name = "Person 4", IsChecked = false }
        };

        DropDownClosedCommand = new DelegateCommand(OnDropDownClosed);

    }

    public event PropertyChangedEventHandler PropertyChanged;

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

    private void OnDropDownClosed(object e)
    {
        SelectedPerson = ComboBoxList.FirstOrDefault(x => x.IsChecked);
    }
}

public class Person
{
    public string Name { get; set; }

    public bool IsChecked { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

Upvotes: 0

Views: 44

Answers (1)

Dishant
Dishant

Reputation: 1595

Finally, I figured out the issue that was preventing ComboBox to set SelectedItem. It was because SelectedPerson was not getting set on UI thread, so I wrap that code in dispatcher and it's working as expected.

Code:

CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
   SelectedPerson = ComboBoxList.FirstOrDefault(x => x.IsChecked);
});

Upvotes: 1

Related Questions