2

I have a solution with a couple of projects. In one project, is my model that has an enum called ModelEnum.

Then in my WPF project I have a ViewModel which has a Dictionary.

And in my ViewModel I have my ValuesDictionary setup as:

    private Dictionary<ModelEnum, string> _valuesDictionary = new Dictionary<ModelEnum, string>();

    public Dictionary<ModelEnum, string> ValuesDictionary
    {
        get { return _valuesDictionary; }
        set { _valuesDictionary = value; OnPropertyChanged(_valuesDictionary); }
    }

In my XAML I have:

xmlns:model="clr-namespace:Model.Data;assembly=Model"
...
<TextBox Text="{Binding Path=ValuesDictionary[(model:ModelEnum)ModelEnum.Enum1].Value}" HorizontalAlignment="Left" Height="29" Margin="90,82,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="50"/>

The following XAML snippet:

(model:ModelEnum)ModelEnum.Enum1

is giving me the error "Parameter type mismatch." I'm confused because I thought I was casting this to the Enum type that it was expecting. I referenced this SO question to try it with no luck.

2 Answers 2

8

replace

(model:ModelEnum)ModelEnum.Enum1].Value

with

(model:ModelEnum)Enum1]

Then try. I hope it will work.

Sign up to request clarification or add additional context in comments.

3 Comments

I think you mean replace the whole [(model:ModelEnum)ModelEnum.Enum1].Value (Note about the .Value) with [(model:ModelEnum)Enum1]. Otherwise the OP may think the new expression is [(model:ModelEnum)Enum1].Value which won't work.
That was it. Thanks! For some reason the xaml editor is still showing a red squiggly line underneath it, but when I tested it, all was good.
@Taran: pfff, I spent all afternoon debugging this. Couldn't find a good answer anywhere until I finally found this! Thanx!
5

Just to add to the potential pitfalls with this, I had issues binding without the explicit "Path="

i.e.

{Binding ValuesDictionary[(model:ModelEnum)Enum1]}

doesn't work, but:

{Binding Path=ValuesDictionary[(model:ModelEnum)Enum1]}

works as expected, although the designer (or maybe ReSharper) still complains about syntax errors.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.