I have a DataGrid in a UserControl and I have added a ContextMenu to the DataGrid.
The XAML is as follows:
<sdk:DataGrid ItemsSource="{Binding Path=GridSource}">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding Path=Name, Mode=OneWay}" Header="Name"/>
<sdk:DataGridTextColumn Binding="{Binding Path=Number, Mode=OneWay}" Header="Number"/>
</sdk:DataGrid.Columns>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu Opened="ContextMenu_Opened">
<toolkit:MenuItem IsEnabled="False">
<toolkit:MenuItem.Icon>
<Image x:Name="menuIcon"/>
</toolkit:MenuItem.Icon>
</toolkit:MenuItem>
<toolkit:Separator />
<toolkit:MenuItem Header="View Agent Route" Click="AgentRoute_Click"/>
<toolkit:MenuItem Header="Live Track" Click="LiveTrack_Click"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</sdk:DataGrid>
If I set the source of the menuIcon Image in XAML using
<Image x:Name="menuIcon" Source="../../Assets/Images/user_green.png"/>
Then the icon is rendered fine but if I try to set it in the ContextMenu_Opened event handler usi开发者_Python百科ng:
private void ContextMenu_Opened(object sender, RoutedEventArgs e)
{
menuIcon.Source = new BitmapImage(new Uri("../../Assets/Images/user_green.png", UriKind.Relative));
}
Nothing shows up, I don't get an error or anything it just doesn't show. I've used the same method to set an ImageSource (using the same actual *.png files) elsewhere in my application, any ideas what's happening here?
Is this due to it being a ContextMenu? On a grid? I can't figure out what's going on.
Try using:-
new Uri("/Assets/Images/user_green.png", UriKind.Relative")
As general rule I would avoid ".." parent paths if at all possible, they only cause headaches. You know that there is a Assets folder at the root of the Xap so start with "/Assets" and go from there.
I got this one working by using a ImageSourceConverter within the contextmenu_opened event.
private void ContextMenu_Opened(object sender, RoutedEventArgs e)
{
ImageSourceConverter converter = new ImageSourceConverter();
menuIcon.Source = (ImageSource)converter.ConvertFromString("../../Assets/Images/user_green.png");
}
You have to pass in an Image. This worked for me:
var mus = new MenuItem {
Header = "Unicorns...",
Icon = new Image {
Source = new BitmapImage(new Uri("/myApp;component/img/unicorns.png",UriKind.Relative))
}
};
精彩评论