-
I want to convert enum type to string to show in a ListView: <ListView x:Name="MyList" ItemTemplate="{StaticResource MyListViewTemplate}" ItemsSource="{x:Bind MyListViewModel.List, Mode=OneWay}" /> The List function of MyListViewModel returns an ObservableVector, and class MyItem contains an enum type defined in IDL:
namespace winrt::MyApp::implementation {
struct MyItem : MyItemT<MyItem> {
private:
MyEnum myEnum;
public:
MyEnum MyEnum();
void MyEnum(MyEnum const& newMyEnum);
};
} Then I override to_hstring function of MyEnum according the UWP sample: namespace winrt {
hstring to_hstring(MyEnum myEnum) {
switch (myEnum) {
case StatusEnum::Monday: return L"Start a new week";
case StatusEnum::Tuesday: return L"Balabala";
default: return L"Error";
}
}
} But wherever I put this function, such as App.h, App.cpp, MainWindow.h, MainWindow.cpp... I would always get errors like: MyItemViewModel.obj : error LNK2005: "struct winrt::hstring __cdecl winrt::to_hstring(enum winrt::MyApp::MyEnum)" (?to_hstring@winrt@@YA?AUhstring@1@W4MyEnum@MyApp@1@@Z) has been defined in MyItem.obj It seems C++/WinRT would always generate default to_hstring function of enum class, so how to override it to show in XAML UI? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You probably want a data binding converter - https://docs.microsoft.com/en-us/windows/winui/api/microsoft.ui.xaml.data.binding.converter?view=winui-3.0#Microsoft_UI_Xaml_Data_Binding_Converter but @jevansaks or @MikeHillberg can tell you more. |
Beta Was this translation helpful? Give feedback.
You probably want a data binding converter - https://docs.microsoft.com/en-us/windows/winui/api/microsoft.ui.xaml.data.binding.converter?view=winui-3.0#Microsoft_UI_Xaml_Data_Binding_Converter but @jevansaks or @MikeHillberg can tell you more.