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 CommonWhale, Today, 09:55 AM
          1 response
          2 views
          0 likes
          Last Post NinjaTrader_Erick  
          Started by Gerik, Today, 09:40 AM
          2 responses
          7 views
          0 likes
          Last Post Gerik
          by Gerik
           
          Started by RookieTrader, Today, 09:37 AM
          2 responses
          12 views
          0 likes
          Last Post RookieTrader  
          Started by alifarahani, Today, 09:40 AM
          1 response
          7 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by KennyK, 05-29-2017, 02:02 AM
          3 responses
          1,285 views
          0 likes
          Last Post NinjaTrader_Clayton  
          Working...
          X