danrer
Topic Author
Posts: 26
Joined: 27 Apr 2019, 18:20

ContextMenu incorrect behavior

07 Oct 2022, 21:50

When I try to implement my MenuItem I get behavior different from c#
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);
	}
};
	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)
	);
            <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>
First screenshot - c++
Second screenshot - c#
Attachments
Screenshot_2.png
Screenshot_1.png
 
danrer
Topic Author
Posts: 26
Joined: 27 Apr 2019, 18:20

Re: ContextMenu incorrect behavior

10 Oct 2022, 16:22

Perhaps there is an example of how to do it dynamically?
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: ContextMenu incorrect behavior

11 Oct 2022, 10:51

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):
            <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>
Could you check that?
 
danrer
Topic Author
Posts: 26
Joined: 27 Apr 2019, 18:20

Re: ContextMenu incorrect behavior

11 Oct 2022, 11:49

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):
            <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>
Could you check that?
This helped, but there is another problem, the Header remains empty.
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
						)
				);
#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; \
}
I am confident in my properties as they work fine in other models.
Attachments
Screenshot_2.png
 
danrer
Topic Author
Posts: 26
Joined: 27 Apr 2019, 18:20

Re: ContextMenu incorrect behavior

11 Oct 2022, 17:03

Bug fixed, one of the components was not registered
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: ContextMenu incorrect behavior

13 Oct 2022, 10:53

Thanks for the update, marking this as solved.

Who is online

Users browsing this forum: Ahrefs [Bot] and 9 guests