我有一些XAML:
<StackPanel Grid.Row="1" x:Name="avlLegend" Orientation="Horizontal" >
<sdk:Label Grid.Row="1" Height="20" HorizontalAlignment="Left" Foreground="White" Name="lblLegend" Content="Legend:" FontWeight="Black" VerticalAlignment="Center" Width="57" Margin="12,8,0,11" />
<ItemsControl Grid.Row="1" Name="avlLegendItems" ItemsSource="{Binding Path=AVLAlertLegend}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
我用它来绑定:
private ObservableCollection<AVLAlert> _AVLAlertLegendCollection = new ObservableCollection<AVLAlert>();
public ObservableCollection<AVLAlert> AVLAlertLegendCollection
{
get { return _AVLAlertLegendCollection; }
set
{
_AVLAlertLegendCollection = value;
RaisePropertyChanged("AVLAlertLegendCollection");
}
}
public ObservableCollection<FrameworkElement> AVLAlertLegend
{
get { return _AVLAlertLegend; }
set
{
_AVLAlertLegend = value;
RaisePropertyChanged("AVLAlertLegend");
}
}
public void LoadAvlLegend()
{
foreach (AVLAlert avl in AVLAlertLegendCollection)
{
Image img = new Image();
img.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(String.Format(@"/{0}.png",avl.AlertIconColor), UriKind.RelativeOrAbsolute));
img.Height = 25;
img.Width = 25;
img.Margin = new Thickness(15, 0, 0, 0);
DropShadowEffect dropShadow = new DropShadowEffect();
dropShadow.BlurRadius = 3;
dropShadow.Color = Colors.White;
dropShadow.Direction = 0;
dropShadow.Opacity = 1.0;
dropShadow.ShadowDepth = 0;
img.Effect = dropShadow;
AVLAlertLegend.Add(img);
Label lbl = new Label();
lbl.Content = avl.Description;
lbl.Foreground = new SolidColorBrush(Colors.White);
lbl.Margin = new Thickness(10, 0, 0, 0);
AVLAlertLegend.Add(lbl);
}
}
首次进入视图时,它会加载正常。但是当我离开并回到视野时,我得到的元素已经是另一个元素的孩子了。错误。我做错了什么?