WPF: use enum as index for Binding
-
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
-
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
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
-
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
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[^]
-
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
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
-
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
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
-
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[^]
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
-
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
-
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