【问题标题】:WPF - Hiding a ComboBox SelectionWPF - 隐藏组合框选择
【发布时间】:2019-08-02 18:38:18
【问题描述】:

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

到目前为止,我已经尝试将 selectedImageIndex 设置为 -1,一旦在用户进行选择时设置了 selectedImageSource,但这不起作用,因为默认情况下,[0] 处的第一个图像仍显示在组合框中。我正在使用 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);

        }
    }

【问题讨论】:

标签: c# wpf xaml


【解决方案1】:

在选择 ComboBox 的一个项目后将选择更改回 null 不是一个好习惯,因为如果您需要使用选择的值,它不再可用。

一种解决方案可能是为 ComboBox 的选定项目使用不同的模板。这样,您可以删除图像,但在其位置放置其他内容,例如文本,以便用户了解选择了什么项目。这是以前的 StackOverflow 帖子,解释了如何执行此操作:

Can I use a different Template for the selected item in a WPF ComboBox than for the items in the dropdown part?

我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2015-08-10
    • 1970-01-01
    • 2022-07-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 2013-02-28
    相关资源
    最近更新 更多