Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. WPF: use enum as index for Binding

WPF: use enum as index for Binding

Scheduled Pinned Locked Moved C#
wpfcsharpdatabasewcftutorial
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LionAM
    wrote on last edited by
    #1

    Hello, I wanted to use a (flag) enum as index to access the single flag values. Instead of

    I hoped that I could use something like

    with some helper object providing an index operator. The first works, the second does not...

    Additionally: Can I use an expression (for example an extension method

    TestEnum.Enum1.DoSomething()

    (which returns a different enum item of the same type) instead of the enum item name for the Binding?

    Alex

    P L 2 Replies Last reply
    0
    • L LionAM

      Hello, I wanted to use a (flag) enum as index to access the single flag values. Instead of

      I hoped that I could use something like

      with some helper object providing an index operator. The first works, the second does not...

      Additionally: Can I use an expression (for example an extension method

      TestEnum.Enum1.DoSomething()

      (which returns a different enum item of the same type) instead of the enum item name for the Binding?

      Alex

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Alex, in order to use an enum, you actually need to provide some instance information to WPF for it to be able to "understand" it. To do that, you need to set up an ObjectDataSource that binds in the enum. There's an example here[^] that shows how to do this. You might also want to look at this[^] handy article. BTW - you really should have asked this in the WPF forum. This has nothing to do with C#.

      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

      L 1 Reply Last reply
      0
      • L LionAM

        Hello, I wanted to use a (flag) enum as index to access the single flag values. Instead of

        I hoped that I could use something like

        with some helper object providing an index operator. The first works, the second does not...

        Additionally: Can I use an expression (for example an extension method

        TestEnum.Enum1.DoSomething()

        (which returns a different enum item of the same type) instead of the enum item name for the Binding?

        Alex

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        I dunno anything about WPF, but to answer the question; yes to all. To answer the questions in reverse order;

        LionAM wrote:

        Additionally: Can I use an expression (for example an extension method

        TestEnum.Enum1.DoSomething()

        Yes, see example-code below (tested under Mono);

        public enum TestEnum: int
        {
        Enum1 = 3,
        EnumZwei = 9
        }

        public static class MyExt
        {
        public static TestEnum DoSomething(this TestEnum e)
        {
        return TestEnum.EnumZwei;
        }
        }
        // and then calling it, which looks plain weird;
        class Program
        {
        public static void Main (string[] args)
        {
        Console.WriteLine(TestEnum.Enum1.DoSomething());
        }
        }

        LionAM wrote:

        I hoped that I could use something like

        <CheckBox IsChecked="{Binding Obj[TestEnum.Enum1]}" ...

        with some helper object providing an index operator. The first works, the second does not...

        With a little helper you can. Add the method below;

        public static class MyExt
        {
        public static string Parse(this string SomeString)
        {
        foreach (var enumVal in Enum.GetValues(typeof(TestEnum)))
        {
        string enumValAsString = String.Format("{0}.{1}",
        enumVal.GetType().Name, enumVal);
        int enumValAsInt = (int)enumVal;
        if (SomeString.Contains(enumValAsString))
        {
        SomeString = SomeString.Replace(enumValAsString, enumValAsInt.ToString());
        }
        }
        return SomeString;
        }
        }
        // and an example on using that;
        class Program
        {
        public static void Main (string[] args)
        {
        string s = "{Binding Obj[TestEnum.Enum1]}";
        Console.WriteLine(s);
        Console.WriteLine(s.Parse());
        }
        }

        Enjoy :)

        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

        L 1 Reply Last reply
        0
        • P Pete OHanlon

          Alex, in order to use an enum, you actually need to provide some instance information to WPF for it to be able to "understand" it. To do that, you need to set up an ObjectDataSource that binds in the enum. There's an example here[^] that shows how to do this. You might also want to look at this[^] handy article. BTW - you really should have asked this in the WPF forum. This has nothing to do with C#.

          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          L Offline
          L Offline
          LionAM
          wrote on last edited by
          #4

          Sorry, I missed that there is an WPF forum (hidden behind Silverlight...). Indeed, the samples shown in your links do it the other way round - get the string from the enum value. I needed the value from the string. My error was simply to use the full name

          "{Binding Obj[TestEnum.Enum1]}"

          instead of simply

          "{Binding Obj[Enum1]}"

          The latter works now. Alex

          P 1 Reply Last reply
          0
          • L LionAM

            Sorry, I missed that there is an WPF forum (hidden behind Silverlight...). Indeed, the samples shown in your links do it the other way round - get the string from the enum value. I needed the value from the string. My error was simply to use the full name

            "{Binding Obj[TestEnum.Enum1]}"

            instead of simply

            "{Binding Obj[Enum1]}"

            The latter works now. Alex

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            I'm glad you've got it working.

            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            L 1 Reply Last reply
            0
            • L Lost User

              I dunno anything about WPF, but to answer the question; yes to all. To answer the questions in reverse order;

              LionAM wrote:

              Additionally: Can I use an expression (for example an extension method

              TestEnum.Enum1.DoSomething()

              Yes, see example-code below (tested under Mono);

              public enum TestEnum: int
              {
              Enum1 = 3,
              EnumZwei = 9
              }

              public static class MyExt
              {
              public static TestEnum DoSomething(this TestEnum e)
              {
              return TestEnum.EnumZwei;
              }
              }
              // and then calling it, which looks plain weird;
              class Program
              {
              public static void Main (string[] args)
              {
              Console.WriteLine(TestEnum.Enum1.DoSomething());
              }
              }

              LionAM wrote:

              I hoped that I could use something like

              <CheckBox IsChecked="{Binding Obj[TestEnum.Enum1]}" ...

              with some helper object providing an index operator. The first works, the second does not...

              With a little helper you can. Add the method below;

              public static class MyExt
              {
              public static string Parse(this string SomeString)
              {
              foreach (var enumVal in Enum.GetValues(typeof(TestEnum)))
              {
              string enumValAsString = String.Format("{0}.{1}",
              enumVal.GetType().Name, enumVal);
              int enumValAsInt = (int)enumVal;
              if (SomeString.Contains(enumValAsString))
              {
              SomeString = SomeString.Replace(enumValAsString, enumValAsInt.ToString());
              }
              }
              return SomeString;
              }
              }
              // and an example on using that;
              class Program
              {
              public static void Main (string[] args)
              {
              string s = "{Binding Obj[TestEnum.Enum1]}";
              Console.WriteLine(s);
              Console.WriteLine(s.Parse());
              }
              }

              Enjoy :)

              Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

              L Offline
              L Offline
              LionAM
              wrote on last edited by
              #6

              Thank you for your answer. However, I have to admit that the C# forum was the wrong forum to ask my question. The Binding expression is not used within C# code but within a (static) .xaml file - although it should in principle be possible to assign the Binding string dynamically, I would avoid it for coding style reasons. Alex

              L 1 Reply Last reply
              0
              • P Pete OHanlon

                I'm glad you've got it working.

                *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                L Offline
                L Offline
                LionAM
                wrote on last edited by
                #7

                And for the expression

                {Binding Obj[Enum1.DoSomething()]}

                I found a simple workaround for my case. I simply use multiple properties:

                {Binding ObjDoSomething[Enum1]}
                {Binding ObjDoSomethingElse[Enum1]}

                which internally call the extension method. Alex

                1 Reply Last reply
                0
                • L LionAM

                  Thank you for your answer. However, I have to admit that the C# forum was the wrong forum to ask my question. The Binding expression is not used within C# code but within a (static) .xaml file - although it should in principle be possible to assign the Binding string dynamically, I would avoid it for coding style reasons. Alex

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  You're welcome :)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups