-1

I have an array parameter of type Parameter in the namespace Parameter.Model. I try to bind to a specific value. If I do the following it works without any problems:

...
xmlns:p="clr-namespace:Parameter.Model;assembly=Parameter"
...
<Button Content="{Binding Path=.[1].GWert, Source={x:Static p:Parameterliste.parameter},Mode=TwoWay}"/>

Now I want to define the index with my Enum parBez:

namespace Parameter.Model
{
    public enum parBez : int
    {
        Val1,
        Val2,
        Val3,
    }
}

I tried the suggestions for the converter parameter from that post

As example I tried the following two lines to display the Enum in a Button as a test:

<Button Content="{Binding Path=x:Static p:parBez.Val1,Mode=TwoWay}"/>

and

<Button Content="{Binding Path=.Val1, Source={x:Static p:parBez},Mode=TwoWay}"/>

but it did not work.

  • The first option does compile but does not display any text in the button. There is no message displayed in the output as well.
  • The second option does not compile and shows the following compiler errors:

  • Invalid property path syntax

  • 'p:parBez' member is not valid because it does not have a qualifying type name.

  • Cannot find the member "parBez" on the target type

Does anybody can give me a hint what I do wrong or which is the solution to use the Enum in XAML and use it as a index for my array?

I checked also this post but I think the ValueConverter is not a solution for me because the NotifyEvent is missed if the ValueConverter converts the Enum Value.

Hope that someone had this problem before and could advise me. Thanks in advance for every hint.

Nelf
  • 19
  • 5
  • 1
    It really would help if you explained what “it did not work” means. Did you get an exception? Tell us the exception. A compiler error? [Tell us the error](https://idownvotedbecau.se/itsnotworking/). – Dour High Arch Nov 12 '19 at 17:34
  • Yes you are right, thanks for the tipp. I edit the question. – Nelf Nov 13 '19 at 08:49

1 Answers1

0

This is the correct syntax:

<Button xmlns:p="clr-namespace:Parameter.Model" Content="{x:Static p:parBez.Val1}"/>

This, i.e. setting the Source of a Binding to your enumeration value, also works:

<Button xmlns:p="clr-namespace:Parameter.Model" Content="{Binding Source={x:Static p:parBez.Val1}}"/>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • If I enter your definition for p it shows me the error "The name "parBez" does not exist in the namespace "clr-namespace:Parameter.Model" and it does not compile anymore. The XAML view is in the project Application and the Enum and corresponding file is the project Parameter which are both combined in a solution. The output of Parameter is a DLL. If I use my definition for p and your Content definition it does compile but shows the text "VAL1" in the Content instead if the Enum number 0. My goal is to get the enum number as index of my array in the XAML definition. Do you have another hint? – Nelf Nov 14 '19 at 08:24
  • @Nelf: You didn't mention anything about getting the number in your original post, did you? Why are you using an enum if you want an int? – mm8 Nov 18 '19 at 10:33
  • @Nelf: And if the enum is defined in another project, you should add the assembly name to the nameapace declaration: `xmlns:p="clr-namespace:Parameter.Model;assembly=Parameter"`. – mm8 Nov 18 '19 at 10:34
  • Hello mm8, first I want to thank you for your answer. I did added the assembly to the namespace declaration as written in the begining question and the result is that the name of the enum variable ("Val1") is written to my Button as text. Please have a look to my beginning question: at the end I want to use the enum value as index for the array parameter. My post with the button text was just a test for bether understanding. I Hope you can help me again and give me another hint. – Nelf Nov 20 '19 at 12:00
  • @Nelf: You get the `ToString()` representation of the enum value which is not an `int`. – mm8 Nov 20 '19 at 12:11
  • Thanks for your answer again. Do you have any idea how I can implement the enum that it fits for my project? I used an enum because sometimes I need to acces some items from the parameter structure by code which is quite easier with the enums instead of the numbers. When I asked my question I added new items to the beginning of my structure parameter and to the enum and the values in XAML did not fit anymore because the are coded as numbers. That is why I want to use a dynamic enum instead of int indexes in XAML. – Nelf Nov 20 '19 at 12:55
  • You may use a converter that converts (casts) the enum value to an `int`. – mm8 Nov 20 '19 at 13:19
  • I thought that if I use a converter the variable/converter is only triggered by the onChanged Event when the value of the index/enum changes. That should not happen during runtime, only one time at start. think that the variable will not be triggered and updated when the binded value "GWert" of the item in parameter does change. Or am I wrong? – Nelf Nov 20 '19 at 13:25
  • The converter would be triggered when the binding in my second example is resolved. – mm8 Nov 20 '19 at 13:29
  • Thanks for your response again. I do not understand how this should work: This was what I want to achieve. How do I need to define that index that this works? If I write the number instead of the enum value the converter gets the number from for example Parameterliste.parameter[1].GWert but not the value of the enum. – Nelf Nov 20 '19 at 13:52