带有 CompositeCollection 的 WPF ComboBox - SelectedIndex 不粘连

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18364557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:28:39  来源:igfitidea点击:

WPF ComboBox with CompositeCollection - SelectedIndex not sticking

c#wpfxaml

提问by cogumel0

I'm using a ComboBox with a CompositeCollection as follows:

我将 ComboBox 与 CompositeCollection 一起使用,如下所示:

<ComboBox>
    <ComboBox.ItemsSource>
        <CompositeCollection>
            <ComboBoxItem Content="All"></ComboBoxItem>
            <CollectionContainer Collection="{Binding Source={StaticResource AllBitsSource}}" />
        </CompositeCollection>
    </ComboBox.ItemsSource>
</ComboBox>

The data displayed is exactly as expected, only I now want to set the default index/value/item to be that of the ComboBoxItem with content All and am having some problems.

显示的数据完全符合预期,只是我现在想将默认索引/值/项设置为内容为 All 的 ComboBoxItem 的索引/值/项,但我遇到了一些问题。

If I set:

如果我设置:

<ComboBoxItem Content="All" IsSelected="True"/>

This gets completely ignored.

这完全被忽略了。

I also tried doing:

我也试过这样做:

<ComboBox SelectedIndex="0">

And whilst this does select the All value, when I open the drop down list the value that is highlighted is the very last value to have been loaded onto the ComboBox, and not the All value.

虽然这确实选择了 All 值,但当我打开下拉列表时,突出显示的值是已加载到 ComboBox 的最后一个值,而不是 All 值。

How can I fix this so that my ComboBoxItem content stays selected after the databinding?

如何解决此问题,以便我的 ComboBoxItem 内容在数据绑定后保持选中状态?

EDIT:

编辑:

I have just tried replacing my <CollectionContainer>with another <ComboBoxItem>and it works fine that way, even though they're still inside the <CompositeCollection>.

我刚刚尝试用<CollectionContainer>另一个替换我的<ComboBoxItem>,即使它们仍在<CompositeCollection>.

EDIT2:

编辑2:

Image showing what the problem is:

显示问题所在的图像:

Image

图片

EDIT3:

编辑3:

Code for the AllBitsSource:

AllBitsSource 的代码:

XAML:

XAML:

<Window.Resources>
    <CollectionViewSource x:Key="AllBitsSource" Source="{Binding Path=AllBits}" />

Code behind:

后面的代码:

private readonly ObservableCollection<string> _bits = new ObservableCollection<string>();

private void GetCurrentSettings()
{
    setttings = display.GetDisplaySettings();

    foreach (var mode in setttings)
    {
        var displaySettingInfoArray = mode.GetInfoArray();

        if (_bits.Contains(displaySettingInfoArray[4]) == false)
        {
            _bits.Add(displaySettingInfoArray[4]);
        }
    }
}

public ObservableCollection<string> AllBits
{
    get { return _bits; }
}

GetCurrentSettings()is called on Main()

GetCurrentSettings()被召唤 Main()

回答by Nick

Since you're adding to your Collection after the ComboBox is constructed, you may have to dip into the Loaded event and set your SelectedIndex there...

由于您是在 ComboBox 构建后添加到您的集合中,因此您可能必须深入到 Loaded 事件中并在那里设置您的 SelectedIndex ......

<ComboBox Loaded="ComboBox_Loaded">
    <ComboBox.ItemsSource>
        <CompositeCollection>
            <ComboBoxItem Content="All" />
            <CollectionContainer Collection="{Binding Source={StaticResource AllBitsSource}}" />
        </CompositeCollection>
    </ComboBox.ItemsSource>
</ComboBox>

Code behind:

后面的代码:

private void ComboBox_Loaded(object sender, RoutedEventArgs e)
{
    (sender as ComboBox).SelectedIndex = 0;
}