我正在尝试在堆栈面板中添加存储在 ..\Resources\ebi.png 中的图像。大多数情况下,同一图像将显示在 Stackpanel 中,具体取决于文本框输入“EtReqCount”。以下是尝试过的示例代码,但出现错误提示
“指定的视觉对象已经是另一个视觉对象的子对象或 CompositionTarget 的根”
以下是尝试过的代码:
private BitmapImage bmp = new BitmapImage(new Uri("WpfApplication1;component/Resources/ebi.png", UriKind.RelativeOrAbsolute));
private void EtReqCount_TextChanged(object sender, TextChangedEventArgs e)
{
StackPanel dynamicStackPanel = new StackPanel();
dynamicStackPanel.Width = 300;
dynamicStackPanel.Height = 200;
dynamicStackPanel.Background = new SolidColorBrush(Colors.LightBlue);
dynamicStackPanel.Orientation = Orientation.Vertical;
if (EtReqCount.Text != "")
{
for (int k = 1; k <= Int32.Parse(EtReqCount.Text); k++)
{
Image img = new System.Windows.Controls.Image(); // This makes the difference.
img.Source = bmp;
dynamicStackPanel.Children.Add(img);
}
}
}
XAML 代码: