简体   繁体   中英

How to bind to a Flag variable to a Xceed CheckComboBox

Given an object with a flag variable:

[Flags]
public enum fCondition
    {   
        Scuffed = 1,       
        Torn = 2,
        Stained = 4
    }

   public class AnEntity  : TableEntity, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        // This method is called by the Set accessor of each property.  
        // The CallerMemberName attribute that is applied to the optional propertyName  
        // parameter causes the property name of the caller to be substituted as an argument.  
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public int ConditionID { get; set; }
        public fCondition Condition
        {
            get => (fCondition)ConditionID;
            set {
                if (value != (fCondition)this.ConditionID)
                {
                    this.ConditionID = (int)value;
                    NotifyPropertyChanged();
                }
            }
        }
}

An ObjectDataProvider to access the Flags

<ObjectDataProvider x:Key="enmCondition" MethodName="GetValues" ObjectType="{x:Type core:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="enm:fCondition"></x:Type>
            </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>   

And the Xceed CheckComboBox

<xctk:CheckComboBox  ItemsSource="{Binding Source={StaticResource enmCondition}}" />

At this stage it is showing the list of Flags with the check box. I would have assumed that I would need to:

SelectedItemsOverride="{Binding Path=Condition, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

I have tried what seems to me to be all other logical combinations (not to metion some ones that seem illogical too, any help would be appreciated).

How are you supposed to bind several checked items to a single Condition property? You can't.

SelectedItemsOverride should be bound to an IList source property:

 public List<fCondition> SelectedConditions { get; } = new List<fCondition>();

XAML:

<xctk:CheckComboBox  ItemsSource="{Binding Source={StaticResource enmCondition}}" 
                     SelectedItemsOverride="{Binding SelectedConditions}" />

If you only want to select a single value, you should use an ordinary ComboBox :

<ComboBox ItemsSource="{Binding Source={StaticResource enmCondition}}" 
          SelectedItem="{Binding Condition}" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM