Hello,
Welcome to Microsoft Q&A!
You could try to use the VisualTreeHelper
to go down from the ContentDialog
to the child
, to find the Image control, and then you could change its source.
public static T FindChild<T>(DependencyObject parent) where T : DependencyObject
{
if (parent == null)
return null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is T typedChild)
return typedChild;
// Recursively search deeper into nested containers
T result = FindChild<T>(child);
if (result != null)
return result;
}
return null; // Child not found
}
private void ContentDialog_Loaded(object sender, RoutedEventArgs e)
{
ContentDialog contentDlg = sender as ContentDialog;
Image img = FindChild<Image>(contentDlg);
if (img != null)
{
img.Source = new BitmapImage(new Uri("ms-appx:///……"));
}
}
However, you couldn't change that image source before the ShowAsync. The reason is that before ShowAsync, there was no ContentDialog in the Visual Tree. Thank you.
Jeanine
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.