开发者

wpf enum databinding

开发者 https://www.devze.com 2023-04-02 05:46 出处:网络
I have a class BatchInfoViewModel that contains an enum: namespace MyStuff.ViewModel { public class BatchInfoViewModel : ObservableObject

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.


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号