Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Enums with [Flags]

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Enums with [Flags]

    In NT7 I could use enum with flags as properties.

    example:


    [Flags]
    public enum NewsImpact
    {
    High = 0x01,
    Medium = 0x02,
    Low = 0x04
    }


    and property:

    [Editor(typeof(Infragistics.Shared.FlagsEnumUITypeE ditor),typeof(UITypeEditor))]
    public Zweistein.NewsImpact Impact {
    get { return Zweistein.NewsUpdate.NewsImpact; }
    set { Zweistein.NewsUpdate.NewsImpact = value; }
    }


    In NT8 , how do I edit properties of a enum with Flags?


    Andreas

    #2
    Hello Andreas,

    Thanks for your post.

    Could you clarify what you are looking for with a screenshot of the UI?
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Ui is like this








      Enums with flags can have several enum values selected and not only 1 value.

      The above code works in a windows Forms app, but not any more in wpf

      Comment


        #4
        Hello Andreas,

        Thanks for your patience while we looked into this.

        Regrettably we can find no such conversion and we do not have a concept like this in the new UI design that could be used in this manner. It is suggested to use normal bools.

        Perhaps others in the community may be able to contribute alternatives.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by zweistein View Post
          Ui is like this








          Enums with flags can have several enum values selected and not only 1 value.

          The above code works in a windows Forms app, but not any more in wpf
          Some time back, I found this somewhere on the internet. Unfortunately, I do not remember exactly where. So, while I cannot properly attribute this to to original author, I can say for certain that this is not my original work. (IOW, no plagiarism is intended.)
          Code:
          /// <summary>
          /// Two-way conversion from flags to bool and back using parameter as mask
          /// Warning: The trick is in storing value locally between calls to Convert and ConvertBack
          /// You must have a single instance of this converter per flags property per object
          /// Do not share this converter between different objects or properties
          /// Typical usage:
          /// [Flags] enum FlagType { None = 0, Trade = 1, Quote = 2, Report = 4, All = 255 }
          /// <local:EditableFlagsToBooleanConverter x:Key="FlagsToBooleanConverter" />
          /// <CheckBox IsChecked="{Binding Prop1, Converter={StaticResource FlagsToBooleanConverter}, Mode=TwoWay, 
          ///     ConverterParameter={x:Static local:FlagType.Trade}}" >Trade</CheckBox>
          /// <CheckBox IsChecked="{Binding Prop1, Converter={StaticResource FlagsToBooleanConverter}, Mode=TwoWay, 
          ///     ConverterParameter={x:Static local:FlagType.Quote}}" >Quote</CheckBox>
          /// <CheckBox IsChecked="{Binding Prop1, Converter={StaticResource FlagsToBooleanConverter}, Mode=TwoWay, 
          ///     ConverterParameter={x:Static local:FlagType.Report}}" >Report</CheckBox>
          /// </summary>
          public class EditableFlagsToBooleanConverter : IValueConverter
          {
              private ulong _target;
          
              public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
              {
                  if (parameter is Enum && value is Enum)
                  {
                      var mask = (ulong) parameter;
                      _target = (ulong) value;
                      return ((mask & _target) != 0);
                  }
          
                  return Binding.DoNothing;
              }
          
              public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
              {
                  if (value is bool && parameter is Enum)
                  {
                      var mask = (ulong)parameter;
                      if ((bool)value)
                      {
                          _target |= mask;
                      }
                      else
                      {
                          _target &= ~mask;
                      }
                      return _target;
                  }
          
                  return Binding.DoNothing;
              }
          }

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by yertle, Yesterday, 08:38 AM
          7 responses
          28 views
          0 likes
          Last Post yertle
          by yertle
           
          Started by bmartz, 03-12-2024, 06:12 AM
          2 responses
          20 views
          0 likes
          Last Post bmartz
          by bmartz
           
          Started by funk10101, Today, 12:02 AM
          0 responses
          4 views
          0 likes
          Last Post funk10101  
          Started by gravdigaz6, Yesterday, 11:40 PM
          1 response
          8 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by MarianApalaghiei, Yesterday, 10:49 PM
          3 responses
          10 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Working...
          X