2

I have a class BatchInfoViewModel that contains an enum:

namespace MyStuff.ViewModel
{
    public class BatchInfoViewModel : ObservableObject
    {
        public enum TimeFrame
        {
            Today,
            Last7days,
            Last30days,
            Last6months,
            Last12months,
            All
        }
    }
}

and a user control 'BatchInfoView' that uses a BatchInfoViewModel and I'm trying to bind a combobox in this view, to the TimeFrame enum on the model, but every resource I've found shows what I think is the method I'm using, but I keep getting Type not found exceptions when running.

<UserControl x:Class="MyStuff.View.BatchInfoView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:view="clr-namespace:MyStuff.View"
         xmlns:viewModel="clr-namespace:MyStuff.ViewModel;assembly=MyStuff.ViewModel"
         xmlns:sys="clr-namespace:System;assembly=mscorlib">    
<UserControl.Resources>
    <ObjectDataProvider x:Key="EnumDataProvider"                              
                        MethodName="GetValues"                              
                        ObjectType="{x:Type sys:Enum}">
        <ObjectDataProvider.MethodParameters>

 <!--None of these work at all, I'm lost :( I've tried variations of these: -->
 <!--<viewModel:BatchInfoViewModel></viewModel:BatchInfoViewModel>
 <x:Type TypeName="viewModel:TimeFrame"/>
 <x:Type TypeName="BatchInfoViewModel:TimeFrame"/>-->


        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>

It can't find the Types and will throw an exception.

1 Answer 1

5

You have an enum nested in a class, place the enum in the namespace, outside of any class and use viewModel:TimeFrame.

(I tested the + concatenation syntax which you can use for x:Static on enums but it does not appear to apply here)

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

2 Comments

Placing the enum in the namespace, outside of any class was the ticket. Thank you !
@cjsmith: Glad that helped, just tested my earlier suggestion but it does not apply here.

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.