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 DJ888, 04-26-2024, 10:57 PM
          2 responses
          16 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by port119, 04-27-2024, 02:43 PM
          3 responses
          31 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by sastrades, 01-31-2024, 10:19 PM
          13 responses
          204 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by reynoldsn, Today, 04:40 PM
          0 responses
          7 views
          0 likes
          Last Post reynoldsn  
          Started by Philippe56140, 04-27-2024, 02:35 PM
          6 responses
          55 views
          0 likes
          Last Post bltdavid  
          Working...
          X