Posted 16 years ago
by Phil Devaney
-
Senior Software Engineer,
Serck Controls Ltd
Version: 4.5.0483
I am trying to create a menu dynamicly using databinding, where the datasource is hierarchical with parent objects mapped onto sub-menus. I can get the menu created using DataTemplates, but the problem is the sub-menu only appears when the PopupButton in the parent menu is clicked instead of when moused over, and doesn't go away when the mouse moves away.
Here's my example XAML:And my code-behind:
I guess something needs to set IsPopupOpen = true on the PopupButton when IsMouseOver == true. I looked in the PopupButtonMenuItemSmallTemplate control template, but couldn't see any triggers to do this. I tried adding a trigger like this:
but it doesn't work properly - the sub-menu appears, but when the mouse moves away from the parent item the entire menu disappears.
Phil
Here's my example XAML:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:r="http://schemas.actiprosoftware.com/winfx/xaml/ribbon"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1" SizeToContent="WidthAndHeight"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
<DataTemplate DataType="{x:Type local:Foo}">
<r:Button Context="MenuItem" Label="{Binding Name}" />
</DataTemplate>
<DataTemplate DataType="{x:Type local:Bar}">
<r:Button Context="MenuItem" Label="{Binding Name}" />
</DataTemplate>
<DataTemplate DataType="{x:Type local:FooBar}">
<r:PopupButton Context="MenuItem" Label="{Binding Name}">
<r:PopupButton.PopupContent>
<r:Menu ItemsSource="{Binding Bars}" />
</r:PopupButton.PopupContent>
</r:PopupButton>
</DataTemplate>
</Window.Resources>
<StackPanel Orientation="Horizontal" Margin="50">
<r:PopupButton Label="Databound Menu" Padding="10">
<r:PopupButton.PopupContent>
<r:Menu ItemsSource="{Binding Foos}" />
</r:PopupButton.PopupContent>
</r:PopupButton>
</StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Windows;
namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
public IList<Foo> Foos
{
get
{
return new Foo[]
{
new Foo() { Name = "Foo 1" },
new Foo() { Name = "Foo 2" },
new FooBar()
{
Name = "FooBar 1",
Bars =
{
new Bar() { Name = "Bar 1" },
new Bar() { Name = "Bar 2" },
}
},
new Foo() { Name = "Foo 3" },
};
}
}
}
public class Foo
{
public string Name { get; set; }
}
public class Bar
{
public string Name { get; set; }
}
public class FooBar : Foo
{
public FooBar()
{
this.Bars = new List<Bar>();
}
public List<Bar> Bars { get; private set; }
}
}
<r:PopupButton.Style>
<Style>
<Style.Triggers>
<Trigger Property="r:PopupButton.IsMouseOver" Value="True">
<Setter Property="r:PopupButton.IsPopupOpen" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</r:PopupButton.Style>
Phil