I have a WPF form set up as follows;
<ListBox x:Name="lbModules" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Command="{Binding OnClick}">
<StackPanel>
<Image Source="{Binding ModuleIcon}"/>
<Label Content="{Binding ModuleName}"/>
</StackPanel>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel></WrapPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
In the code behind, lbModules is given a List<ModuleButton> as the ItemsSource, where ModuleButton is defined as follows;
internal class ModuleButton
{
public ImageSource ModuleIcon {get; set;}
public string ModuleName {get; set;}
public ICommand OnClick {get; set;}
}
My problem is defining the OnClick command in a dynamic fashion. I need to do this as I am using MEF and the OnClick event is technically in a different assembly. I just need to call module.GetForm() but it doesn't seem to be quite that simple...
I build the ModuleButton(s) as follows;
Lazy<IModule, IModuleMetadata> moduleCopy = module;
ModuleButton button = new ModuleButton
{
ModuleName = moduleCopy.Metadata.ModuleName,
ModuleIcon =
Imaging.CreateBitmapSourceFromHBitmap(moduleCopy.Value.ModuleButtonIcon.GetHbitmap(),
IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()),
OnClick = // TODO: Somehow call moduleCopy.Value.GetForm()
};
I've been searching far and wide, checking various results in Google, this is one of my latest sources.
Is it possible to do what I'm trying to do? If so, how?