ContextMenu incorrect behavior
When I try to implement my MenuItem I get behavior different from c#
First screenshot - c++
Second screenshot - c#
Code: Select all
class IContectMenuItem : public Noesis::NotifyPropertyChangedBase
{
public:
IContectMenuItem() {}
IContectMenuItem(const wchar_t* header, MenuItemState menuState)
{
IContectMenuItem(WCharToUtf8(header).c_str(), menuState);
}
IContectMenuItem(const char* header, MenuItemState menuState)
{
SetHeader(header);
if (bool(menuState & MenuItemState::Disabled)) {
SetEnabled(false);
}
if (bool(menuState & MenuItemState::Title)) {
SetEnabled(false);
}
}
~IContectMenuItem() {}
CreateStringGetSetProp(Header, 64);
CreateGetSetProp(bool, Enabled);
NS_IMPLEMENT_INTERFACE_FIXUP;
protected:
NS_IMPLEMENT_INLINE_REFLECTION(IContectMenuItem, Noesis::NotifyPropertyChangedBase, "IContectMenuItem")
{
RegGetSetProp(IContectMenuItem, Header);
RegGetSetProp(IContectMenuItem, Enabled);
}
};
Code: Select all
m_contextMenuItems->Clear();
m_contextMenuItems->Add
(
Noesis::MakePtr<IContectMenuItem>("Test 1", MenuItemState::None)
);
m_contextMenuItems->Add
(
Noesis::MakePtr<IContectMenuItem>("Test 1", MenuItemState::None)
);
m_contextMenuItems->Add
(
Noesis::MakePtr<IContectMenuItem>("Test 2", MenuItemState::None)
);
Code: Select all
<Grid.ContextMenu>
<ContextMenu ItemsSource="{Binding Path=ContextMenuItems}">
<ContextMenu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Header" Value="{Binding Path=Header}"/>
</Style>
</ContextMenu.ItemContainerStyle>
</ContextMenu>
</Grid.ContextMenu>
Second screenshot - c#
Re: ContextMenu incorrect behavior
Perhaps there is an example of how to do it dynamically?
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: ContextMenu incorrect behavior
Whenever you see a pink template rendered instead of the normal control, it means that control doesn't have a valid template and it is using our internal debug templates. This happens when you define a local style that doesn't extend your application styles. In this example you should probably do the following (see I added BasedOn in the Style):
Could you check that?
Code: Select all
<Grid.ContextMenu>
<ContextMenu ItemsSource="{Binding Path=ContextMenuItems}">
<ContextMenu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="Header" Value="{Binding Path=Header}"/>
</Style>
</ContextMenu.ItemContainerStyle>
</ContextMenu>
</Grid.ContextMenu>
Re: ContextMenu incorrect behavior
This helped, but there is another problem, the Header remains empty.Whenever you see a pink template rendered instead of the normal control, it means that control doesn't have a valid template and it is using our internal debug templates. This happens when you define a local style that doesn't extend your application styles. In this example you should probably do the following (see I added BasedOn in the Style):Could you check that?Code: Select all<Grid.ContextMenu> <ContextMenu ItemsSource="{Binding Path=ContextMenuItems}"> <ContextMenu.ItemContainerStyle> <Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource {x:Type MenuItem}}"> <Setter Property="Header" Value="{Binding Path=Header}"/> </Style> </ContextMenu.ItemContainerStyle> </ContextMenu> </Grid.ContextMenu>
Code: Select all
m_contextMenuItems->Add
(
Noesis::MakePtr<IContectMenuItem>
(
"Test 1",
MenuItemState::Title | MenuItemState::NotCheckable
)
);
m_contextMenuItems->Add
(
Noesis::MakePtr<IContectMenuItem>
(
"Test 1",
MenuItemState::Title | MenuItemState::NotCheckable
)
);
m_contextMenuItems->Add
(
Noesis::MakePtr<IContectMenuItem>
(
"Test 1",
MenuItemState::Title | MenuItemState::NotCheckable
)
);
Code: Select all
#define CreateStringGetSetProp(name, size) \
private: \
char m_##name[size] = {}; \
public: \
void Set##name(const char* value) \
{ \
if (!Noesis::StrEquals(m_##name, value)) \
{ \
Noesis::StrCopy(m_##name, sizeof(m_##name), value); \
OnPropertyChanged(#name); \
} \
} \
const char* Get##name() const \
{ \
return m_##name; \
}
Re: ContextMenu incorrect behavior
Bug fixed, one of the components was not registered
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: ContextMenu incorrect behavior
Thanks for the update, marking this as solved.
Who is online
Users browsing this forum: Ahrefs [Bot] and 9 guests