Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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# 自动完成框-如何从匹配项集合中选择特定项?_C#_Wpf_Binding_Autocompletebox - Fatal编程技术网

C# 自动完成框-如何从匹配项集合中选择特定项?

C# 自动完成框-如何从匹配项集合中选择特定项?,c#,wpf,binding,autocompletebox,C#,Wpf,Binding,Autocompletebox,使用AutoCompleteBox时,我有一个项目列表,其中包含匹配的显示文本,但ID值不同 [{Text:"John",Id:1},{Text:"John",Id:2},{Text:"John Doe",Id:3}] 当我选择第二行John 2时,第一个值John 1被设置为SelectedItem属性。当选择一个不同的值John Doe 3-它工作正常 我的观察是,如果有更多的匹配项,它总是需要第一个匹配项 如何将正确的项目Joh

使用AutoCompleteBox时,我有一个项目列表,其中包含匹配的显示文本,但ID值不同

[{Text:"John",Id:1},{Text:"John",Id:2},{Text:"John Doe",Id:3}]
当我选择第二行John 2时,第一个值John 1被设置为SelectedItem属性。当选择一个不同的值John Doe 3-它工作正常

我的观察是,如果有更多的匹配项,它总是需要第一个匹配项

如何将正确的项目John 2设置为SelectedItem

问题在于TryGetMatch方法搜索匹配文本,然后选择第一个匹配项

我在传递SelectedItem的地方添加了priorityView参数。因此,在搜索ItemSource的其余部分之前,首先检查当前选择的EdItem是否已与文本匹配:

private object TryGetMatch(string searchText, object priorityView,ObservableCollection<object> view, AutoCompleteFilterPredicate<string> predicate)
{
    if (priorityView != null)
    {
        if (predicate(searchText, FormatValue(priorityView)))
        {
            return priorityView;
        }
    }
    
    if (view != null && view.Count > 0)
    {
        foreach (object o in view)
        {
            if (predicate(searchText, FormatValue(o)))
            {
                return o;
            }
        }
    }

    return null;
}

这也是原始存储库中的内容

您使用的是什么框架>WPF工具箱、Avalon、caliburn?这看起来像是您正在使用的任何第三方库的实现问题。该绑定看起来像是黑客攻击,为什么不绑定到VM?@XAMlMAX感谢您的评论。正因为如此,我才意识到我们使用的是WpfToolkit的副本。然后我去解决了这个问题,并创建了我的第一个拉请求。这个问题现在已经解决了
public class TestItemDto
{
    public int Id { get; set; }
    public string Text { get; set; }
}

private ObservableCollection<TestItemDto> _testItemSource = new ObservableCollection<TestItemDto>
{
    new TestItemDto
    {
        Id = 1,
        Text = "John"
    },
    new TestItemDto
    {
        Id = 2,
        Text = "John"
    },
    new TestItemDto
    {
        Id = 3,
        Text = "John Doe"
    },
};

public ObservableCollection<TestItemDto> TestItemSource
{
    get => _testItemSource;
    set
    {
        _testItemSource = value;
    }
}


private string _testText;

public string TestText
{
    get { return _testText; }
    set
    {
        _testText = value;
        Console.WriteLine($"TestText:{TestText}");
    }
}

private TestItemDto _testSelectedItem;

public TestItemDto TestSelectedItem
{
    get { return _testSelectedItem; }
    set
    {
        _testSelectedItem = value;
        Console.WriteLine($"TestText:{TestSelectedItem?.Id}:{TestSelectedItem?.Text}");
    }
}
public class AutocompleteItemTextConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var adresarSearchResultItemDto = value as TestItemDto;
        return adresarSearchResultItemDto?.Text;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}
private object TryGetMatch(string searchText, object priorityView,ObservableCollection<object> view, AutoCompleteFilterPredicate<string> predicate)
{
    if (priorityView != null)
    {
        if (predicate(searchText, FormatValue(priorityView)))
        {
            return priorityView;
        }
    }
    
    if (view != null && view.Count > 0)
    {
        foreach (object o in view)
        {
            if (predicate(searchText, FormatValue(o)))
            {
                return o;
            }
        }
    }

    return null;
}