I am using Mahapp's DropDownButton
Here is what I have:
using
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
<mah:DropDownButton
Content="my button"
Icon="{DynamicResource appbar_projector_screen}"
ItemsSource="{Binding Path=MySource}">
<mah:DropDownButton.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Header" Value="{Binding Path=Title}"/>
<Setter Property="Command" Value="{Binding Path=Command}"/>
<Setter Property="CommandParameter" Value="{Binding Path=Id }"/>
<Setter Property="Icon">
<Setter.Value>
<Image Width="25" Height="25" Source="{Binding IconPath}" />
</Setter.Value>
</Setter>
</Style>
</mah:DropDownButton.ItemContainerStyle>
</mah:DropDownButton>
First I tried using:
<Setter Property="Header">
<Setter.Value>
<StackPanel>
<Image Width="20" Height="20" Source="{Binding IconPath}" />
<TextBlock Text="{Binding Path=Title}" />
</StackPanel>
</Setter.Value>
</Setter>
But that code does not show any image. So then I tried:
<Setter Property="Icon">
<Setter.Value>
<Image Width="25" Height="25" Source="{Binding IconPath}" />
</Setter.Value>
</Setter>
Initializing the Object MySource
:
public List<SomeDefinition> MySource{ get; private set; }
MySource= new List<SomeDefinition>
{
new SomeDefinition{Id=1, Title= $@"1", Command = new RelayCommand<object>(SomeMethod1), IconPath = "D:\ico1.png"},
new SomeDefinition{Id=2, Title= $@"2", Command = new RelayCommand<object>(SomeMethod2), IconPath = "D:\ico2.png"},
new SomeDefinition{Id=3, Title= $@"3", Command = new RelayCommand<object>(SomeMethod3), IconPath = "D:\ico3.png"},
new SomeDefinition{Id=4, Title= $@"4", Command = new RelayCommand<object>(SomeMethod4), IconPath = "D:\ico4.png"},
new SomeDefinition{Id=5, Title= $@"5", Command = new RelayCommand<object>(SomeMethod5), IconPath = "D:\ico5.png"}
};
having:
public class SomeDefinition
{
public int Id { get; set; }
public string Title{ get; set; }
public ICommand Command { get; set; }
public string IconPath { get; set; }
}
This shows image only for the last value on menu, I can′t figure out what I am missing. Why is it showing only image for last record?
I tested with all images and they are written correctly, they also do exist, so issue is not finding the image file .
Update
So I have tried using @mm8 solution and changed definition Class to:
public class SomeDefinition
{
public int Id { get; set; }
public string Title{ get; set; }
public ICommand Command { get; set; }
public Image IconImage { get; set; }
}
having xaml:
<mah:DropDownButton
Content="my button"
Icon="{DynamicResource appbar_projector_screen}"
ItemsSource="{Binding Path=MySource}">
<mah:DropDownButton.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Header" Value="{Binding Path=Title}"/>
<Setter Property="Command" Value="{Binding Path=Command}"/>
<Setter Property="CommandParameter" Value="{Binding Path=Id }"/>
<Setter Property="Icon" Value="{Binding IconImage}" />
</Style>
</mah:DropDownButton.ItemContainerStyle>
</mah:DropDownButton>
and code behind:
var Image = new Bitmap(@"D:\1.png");
MySource = new List<SomeDefinition>
{
new SomeDefinition{Id=1, Title= $@"1", Command = Command1, IconImage = Image},
new SomeDefinition{Id=2, Title= $@"2", Command = Command2, IconImage = Image},
new SomeDefinition{Id=3, Title= $@"3", Command = Command3, IconImage = Image},
new SomeDefinition{Id=4, Title= $@"4", Command = Command4, IconImage = Image},
new SomeDefinition{Id=5, Title= $@"5", Command = Command5, IconImage = Image}
};
Result is:
How to convert object to Image?
See Question&Answers more detail:
os