Firstly, to produce a list of buttons with menus, I am using a ListBox:
<
Grid
>
ListBox
ItemsSource
=
"{Binding Applications}"
ItemTemplate
"{StaticResource ApplicationsTemplate}"
ItemContainerStyle
"{StaticResource NoHighlightStyle}"
ItemsPanel
"{DynamicResource HorizontalItemsPanel}"
/>
</
To list the buttons horizontally, I change the ItemsPanel: <ItemsPanelTemplate x:Key="HorizontalItemsPanel"> <VirtualizingStackPanel Orientation="Horizontal" IsItemsHost="True" VerticalAlignment="Top"/></ItemsPanelTemplate>
To list the buttons horizontally, I change the ItemsPanel:
ItemsPanelTemplate
x:Key
"HorizontalItemsPanel"
VirtualizingStackPanel
Orientation
"Horizontal"
IsItemsHost
"True"
VerticalAlignment
"Top"
Style
"NoHighlightStyle"
TargetType
"{x:Type ListBoxItem}"
Setter
Property
"Template"
Setter.Value
ControlTemplate
Background
"{TemplateBinding Background}"
ContentPresenter
x:Name
"contentPresenter"
ContentTemplate
"{TemplateBinding ContentTemplate}"
Content
"{TemplateBinding Content}"
HorizontalAlignment
"{TemplateBinding HorizontalContentAlignment}"
Margin
"{TemplateBinding Padding}"
DataTemplate
"ApplicationsTemplate"
RadioButton
"tb"
"{Binding Name}"
"{DynamicResource MouseOverPopupRadio}"
GroupName
"GrpApp"
Popup
IsOpen
"{Binding IsChecked, ElementName=tb}"
"{Binding Options}"
"{StaticResource OptionsTemplate}"
"MouseOverPopupRadio"
"{x:Type RadioButton}"
TextBlock
Text
"5"
Padding
"Yellow"
"OptionsTemplate"
Button
IsEnabled
"{Binding IsEnabled}"
Click
"OptionButton_Click"
Below is the class for the options. Because we want to control the IsEnabled property of each option, we use INotifyPropertyChanged to notify the Ui that IsEnabled has changed:
public
class
Option : INotifyPropertyChanged
{
string
Name {
get
;
set
; }
int
ParentIndex {
bool
_IsEnabled;
return
}
if
(_IsEnabled != value)
_IsEnabled = value;
RaisePropertyChanged(
"IsEnabled"
);
void
prop)
(PropertyChanged !=
null
) { PropertyChanged(
this
,
new
PropertyChangedEventArgs(prop)); }
event
PropertyChangedEventHandler PropertyChanged;
When an option button is clicked, we can extrapolate back to find the actual option, its parent application and therefore change the IsEnabled property of the other options, and of course act upon the application depending on the option.
private
OptionButton_Click(
object
sender, RoutedEventArgs e)
var button = sender
as
Button;
var option = button.DataContext
Option;
var application = Applications[option.ParentIndex];
switch
(option.Name)
case
"Share"
:
foreach
(var opt
in
application.Options)
opt.IsEnabled = opt.Name ==
?
false
true
break
default
This is a silly function that simply changed the IsEnabled property, and hence updates the UI, but it shows how I can reference the option, and it's parent class, which allows me to do whatever I need to do.