WPF-隐藏组合选择



我有一个Commobox,该组合允许用户从图像集合中进行选择。由于图像的大小,我希望一旦选择了图像,因此我不想在Combobox中显示图像。选择项目时,我只是想在Combobox中显示任何内容。

到目前为止,我已经尝试将SelectedImageIndex设置为-1,一旦用户进行选择时设置了SelectionImagesource,但是由于[0]的第一个图像仍在ComboBox中显示在Combobox中。我正在使用mvvm。

xaml

<ComboBox Grid.Row="1" SelectedIndex="{Binding SelectedImageIndex}" ItemsSource="{Binding SymbolImageCollection}">
                            <ComboBox.ItemTemplate>
                                <DataTemplate>
                                        <Image Source="{Binding Img}" Width="50" Height="50"/>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                            <ComboBox.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <WrapPanel Width="300" HorizontalAlignment="Left"/>
                                </ItemsPanelTemplate>
                            </ComboBox.ItemsPanel>
                        </ComboBox>

查看模型

public ObservableCollection<SymbolImage> SymbolImageCollection { get { return AppearanceLayerProperties.Instance.SymbolImageCollection; } }
    private string _selectedImageSource;
    public string SelectedImageSource
    {
        get { return _selectedImageSource; }
        set
        {
            SetProperty(ref _selectedImageSource, value);
            //SelectedImageIndex = -1;
        }
    }
    private int _selectedImageIndex;
    public int SelectedImageIndex
    {
        get { return _selectedImageIndex; }
        set
        {
            var selectedImage = AppearanceLayerProperties.Instance.SymbolImageCollection[value].ImgSource;
            SelectedImageSource = selectedImage;
            SetProperty(ref _selectedImageIndex, -1);
        }
    }

选择ComboBox的项目后,将选择更改回NULL不是很好的做法,因为如果您需要使用选定的值,则不再可用。

解决方案可能是为Combobox的选定项目具有不同的模板。这样,您可以删除图像,但是取而代之的是将其他内容(例如文本(放置,因此用户可以作为选择哪些项目的想法。这是以前的stackoverflow文章,说明了如何执行此操作:

我可以在WPF Combobox中使用其他模板与下拉部分中的项目使用其他模板?

我希望这会有所帮助!

最新更新