[英]Silverlight: TargetInvocationException Occurred by Bind UIElement To ListBoxItems
我創建一個ListBox
並將ItemsSource
綁定到對象列表:
MainPage.xaml :
<Grid x:Name="LayoutRoot" Background="White"
DataContext="{StaticResource ResourceKey=ViewModel}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="47*"/>
<ColumnDefinition Width="28*"/>
</Grid.ColumnDefinitions>
<ListBox ItemsSource="{Binding Models}"
Height="200" Width="150" Grid.Column="0">
<ListBox.ItemTemplate>
<DataTemplate >
<StackPanel>
<TextBlock Text="{Binding ID ,Mode=TwoWay
,UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Text="{Binding Name ,Mode=TwoWay
,UpdateSourceTrigger=PropertyChanged}"/>
<Border Child="{Binding Shape ,Mode=TwoWay
,UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Change Source" Height="35" Width="100"
Grid.Column="1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding ChangeSource}"
CommandParameter="{Binding ElementName=LayoutRoot}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
Model.cs :
public class Model
{
private string _ID;
private UIElement _Shape;
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
public UIElement Shape
{
get { return _Shape; }
set { _Shape = value;}
}
public string ID
{
get { return _ID; }
set { _ID = value; }
}
}
和
ViewModel.cs :
public class ViewModel : INotifyPropertyChanged
{
Random rnd = new Random();
List<Color> MyColors = new List<Color>() { Colors.Gray,
Colors.Blue, Colors.Red,
Colors.Green, Colors.Yellow,
Colors.Orange, Colors.DarkGray };
private List<Model> _Models;
public List<Model> Models
{
get
{
if (_Models == null)
_Models = new List<Model>();
return _Models;
}
set { _Models = value; OnPropertyChanged("Models"); }
}
public DelegateCommand<object> ChangeSource { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string PropertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
public ViewModel()
{
ChangeSource = new DelegateCommand<object>(ChangeSourceCommand);
ChangeSourceItems();
}
private void ChangeSourceCommand(object obj)
{
ChangeSourceItems();
}
private void ChangeSourceItems()
{
List<Model> tmpModels = new List<Model>();
tmpModels.Add(new Model() { ID = "1", Name = "A", Shape = new Border() { Background = new SolidColorBrush(MyColors[rnd.Next(0, MyColors.Count - 1)]), Width = 10, Height = 10 } });
tmpModels.Add(new Model() { ID = "2", Name = "B", Shape = new Border() { Background = new SolidColorBrush(MyColors[rnd.Next(0, MyColors.Count - 1)]), Width = 10, Height = 10 } });
tmpModels.Add(new Model() { ID = "3", Name = "C", Shape = new Border() { Background = new SolidColorBrush(MyColors[rnd.Next(0, MyColors.Count - 1)]), Width = 10, Height = 10 } });
tmpModels.Add(new Model() { ID = "4", Name = "D", Shape = new Border() { Background = new SolidColorBrush(MyColors[rnd.Next(0, MyColors.Count - 1)]), Width = 10, Height = 10 } });
tmpModels.Add(new Model() { ID = "5", Name = "E", Shape = new Border() { Background = new SolidColorBrush(MyColors[rnd.Next(0, MyColors.Count - 1)]), Width = 10, Height = 10 } });
tmpModels.Add(new Model() { ID = "6", Name = "F", Shape = new Border() { Background = new SolidColorBrush(MyColors[rnd.Next(0, MyColors.Count - 1)]), Width = 10, Height = 10 } });
Models = tmpModels;
}
}
單擊Button
更改模型的來源。
問題 :
單擊Button
並滾動ListView
之后發生TargetInvocationException
。 為什么?
異常消息 :
調用的目標已引發異常。
InnerException消息 :
值不在預期范圍內。
因為您在模型中使用UIElement
,所以多次創建形狀並滾動發生TargetInvocationException
。
您必須繼承ListBox
並重寫IsItemItsOwnContainerOverride
方法。
protected override bool IsItemItsOwnContainerOverride(object item)
{
Model model = (Model)item;
FrameworkElement elem = (FrameworkElement)model.Shape;
return elem.Parent != null;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.