Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is it possible to use WhenValueConverter to Enum? #1555

Closed
Quico20 opened this issue Aug 26, 2017 · 2 comments
Closed

is it possible to use WhenValueConverter to Enum? #1555

Quico20 opened this issue Aug 26, 2017 · 2 comments

Comments

@Quico20
Copy link

Quico20 commented Aug 26, 2017

I'm developing an app right now and I wanted to exploit as much of template 10 as possible, one of the advantages of using template 10 is that I dont have to write many converters, but I'm trying to use whenvalueconverter to map my enum values.

I think it is not possible but I tried this:

           <converter:ValueWhenConverter x:Key="EnumSexToBooleanConverter">
                <converter:ValueWhenConverter.When>
                    <model:Personal>
                        <model:Personal.Sex>0</model:Personal.Sex>
                    </model:Personal>
                </converter:ValueWhenConverter.When>
                <converter:ValueWhenConverter.Value>
                    <ToggleSwitch>
                        <ToggleSwitch.IsOn>True</ToggleSwitch.IsOn>
                    </ToggleSwitch>
                </converter:ValueWhenConverter.Value>
                <converter:ValueWhenConverter.Otherwise>
                    <ToggleSwitch>
                        <ToggleSwitch.IsOn>False</ToggleSwitch.IsOn>
                    </ToggleSwitch>
                </converter:ValueWhenConverter.Otherwise>
            </converter:ValueWhenConverter>

it doesnt show me an error but the program break whenever i use the toggleswitch like this:

<ToggleSwitch x:Name="SexPersonalInfoToggleSwitch"
                                  RelativePanel.Below="SexPersonalInfoTextBlock"
                                  RelativePanel.AlignLeftWith="SexPersonalInfoTextBlock"
                                  IsOn="{Binding Sex, Mode=TwoWay, Converter={StaticResource EnumSexToBooleanConverter}}"
                                  Toggled="{x:Bind ViewModel.CheckValue, Mode=OneWay}"/>

if I switch the Mode=OneWay the program never breaks, but I also need to convert the enum value whenever IsOn is equals to true or false, my enum has two values Sex.Male, Sex.Female if you convert them to Int are equals 0 and 1.

Is there a way to use this converter in a TwoWay mode with enums or should I stick to use a custom converter to acomplish my goal??

i would appreciate an answer from the experts and sorry to bring this question here instead of stackoverflow but I found someone else made the question there and there wasnt an answer to it.
reference: https://stackoverflow.com/questions/43561663/valuewhenconverter-and-enum

@dg2k
Copy link

dg2k commented Aug 27, 2017

Change the property Sex to a DependencyProperty and try.

 public Gender Sex
 {
     get { return (Gender)GetValue(SexProperty); }
     set { SetValue(SexProperty, value); }
 }

 public static readonly DependencyProperty SexProperty = 
     DependencyProperty.Register(
         nameof(Sex), typeof(Gender),
         typeof([YourContainerType]),
         new PropertyMetadata([initial_default_or_null_here])
     );
 }

 public enum Gender : int
 {
      Female,
      Male
 }

@Quico20
Copy link
Author

Quico20 commented Aug 27, 2017

Thanks buddy, I'll do it!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants