使用自定义ItemTemplate时未设置ComboBox SelectedItem

时间:2018-08-06 23:47:23

标签: c# xaml uwp

我有一个带有自定义ItemTemplate的ComboBox,它有一个CheckBox绑定到该页面的ViewModel。

我要执行的操作是在关闭ComboBox时将选中的ComboBox项设置为第一个选中的值,但是该值不会相应地设置。

有什么我想念的吗?

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;
    }
}

1 个答案:

答案 0 :(得分:1)

最后,我发现了阻止ComboBox设置SelectedItem的问题。这是因为SelectedPerson尚未在UI线程上设置,所以我将代码包装在分派器中,并且按预期工作。

代码:

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