【问题标题】:Don't show the last item in a ComboBox不显示 ComboBox 中的最后一项
【发布时间】:2021-04-28 13:06:03
【问题描述】:

我有一个枚举类型,可以使用数据提供程序绑定到ComboBox,但我想忽略Count 成员。我使用Count 来检查数组的边界,这些数组的项数应与此枚举中存在的类型相同。

enum FingerType
{
    Thumb, Index, Middle, Ring, Pink,
    Count
}

我将此枚举绑定到ComboBox,如下所示:

<UserControl.Resources>
    <ObjectDataProvider x:Key="dataFromEnum" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:FingerType"/>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</UserControl.Resources>

ComboBox如下:

<ComboBox ItemsSource="{Binding Source={StaticResource dataFromEnum}}"/>

那么,我的问题是如何显示除最后一项以外的所有项目?

【问题讨论】:

  • 听起来像XY problem,如果它是无效值,则首先不要有那个枚举值。
  • @Charlieface 不同意。在枚举中使用计数是一种常见的做法。
  • 您可以编写IValueConverter 的实现并在绑定中使用它。
  • 虽然我同意@Charlieface,但您应该考虑创建一个包含您想要的值的替代枚举,并为您建议的枚举创建一个转换器。
  • 可能是常见的做法,但 SQL 注入和缓冲区溢出也是如此。如果错了,那就错了。

标签: c# wpf xaml combobox dataprovider


【解决方案1】:

我认为您可以使用字典以不同的方式解决该问题。下面给出了 viewmodel 的示例 xaml 和 c# 代码。
XAML:

 <ComboBox Grid.Row="2" Grid.Column="7" VerticalAlignment="Top" 
                                               ItemsSource="{Binding FingerTypes, 
                                                           Mode=TwoWay}" 
                                             SelectedValue="{Binding SelectedFingerType, Mode=TwoWay}" 
                                             SelectedValuePath="Key" 
                                             DisplayMemberPath="Value"  
                                             Height="22" Margin="0,3,0,0" />

C# 视图模型

  #region Property FingerTypes: Dictionary<FingerType, string>

        private Dictionary<FingerType, string> _FingerTypes;
        public Dictionary<FingerType, string> FingerTypes
        {
            get
            {
                if (_FingerTypes != null) return _FingerTypes;
                _FingerTypes = new Dictionary<FingerType, string>();

                foreach (FingerType value in Enum.GetValues(typeof(FingerType)))
                {
                    if (value != FingerType.Count)
                        _FingerTypes.Add(value, value.ToString());
                }
                return _FingerTypes;
            }
        }
        #endregion

#region Property SelectedFingerType: ContractExpiryTypeEnum

        private FingerType? _SelectedFingerType;
        public FingerType? SelectedFingerType
        {
            get
            {
                return _SelectedFingerType;
            }
            set
            {
                _SelectedFingerType= value;               
            }
        }
        #endregion

注意:您也可以使用列表,但在 WPF、Silverlight 和 UWP 平台中使用字典对我有用。请检查代码并尝试并告诉我。

【讨论】:

    【解决方案2】:

    我会在 ObjectDataProvider 中使用自己的 EnumHelper 并在其中应用过滤器。

    删除枚举最后一个值的示例

    public class EnumHelper
    {
        public Array FilterValues(Type enumType)
        {
            Array allValues = Enum.GetValues(enumType);
            object[] filteredValues = new object[allValues.Length - 1];
            for (int i = 0; i < allValues.Length-1; i++)
            {
                filteredValues[i] = allValues.GetValue(i);
            }
    
            return filteredValues;
        }
    }
    

    在资源中修改ObjectDataProvider

    <ObjectDataProvider x:Key="dataFromEnum" MethodName="FilterValues" ObjectType="{x:Type local:EnumHelper}">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:FingerType"/>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多