Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 组合框SelectedItem在使用自定义项模板时未设置_C#_Xaml_Uwp - Fatal编程技术网

C# 组合框SelectedItem在使用自定义项模板时未设置

C# 组合框SelectedItem在使用自定义项模板时未设置,c#,xaml,uwp,C#,Xaml,Uwp,我有一个带有自定义ItemTemplate的组合框,它有一个绑定到该页面的ViewModel的复选框 我试图做的是,每当ComboBox关闭时,将ComboBox item selected设置为第一个选中值,但没有相应地设置该值 有什么我遗漏的吗 Xaml: 视图模型: 最后,我发现了阻止ComboBox设置SelectedItem的问题。这是因为SelectedPerson并没有在UI线程上设置,所以我将代码包装在dispatcher中,它按照预期工作 代码: <ComboBox x:

我有一个带有自定义ItemTemplate的组合框,它有一个绑定到该页面的ViewModel的复选框

我试图做的是,每当ComboBox关闭时,将ComboBox item selected设置为第一个选中值,但没有相应地设置该值

有什么我遗漏的吗

Xaml:

视图模型:


最后,我发现了阻止ComboBox设置SelectedItem的问题。这是因为SelectedPerson并没有在UI线程上设置,所以我将代码包装在dispatcher中,它按照预期工作

代码:

<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>
 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;
    }
}
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
   SelectedPerson = ComboBoxList.FirstOrDefault(x => x.IsChecked);
});