我在Visual Studio 2010应用程序中有以下WPF和C#摘录。当我运行我的应用程序并说内容为“{DisconnectedItem}”时,此异常会捕获此代码。对我而言,这意味着我的'loadDesc'函数被调用于当前不在ListView视图中的对象(Checkboxes)。为什么是这样?任何帮助表示赞赏。
下面的C#和WPF代码:
private void loadDesc(object sender, RoutedEventArgs e)
{
try
{
string num = (string)(((sender as TextBlock).Parent as StackPanel).Children[0] as CheckBox).Content;
if (num == null)
{
return;
}
if (num.Equals("Tamper"))
{
(sender as TextBlock).Text = "";
}
else
{
int x = Convert.ToInt32(num);
dc.ext247[x - 1].desc = ZoneDescriptionsVM.getInstance().zoneDesc[x - 1].description.Trim();
dc.onOff247[x - 1].desc = ZoneDescriptionsVM.getInstance().zoneDesc[x - 1].description.Trim();
}
}
catch (Exception ex)
{
MessageBox.Show((((sender as TextBlock).Parent as StackPanel).Children[0] as CheckBox).Content.ToString());
}
}
<ListView KeyboardNavigation.TabNavigation="Continue" VirtualizingStackPanel.VirtualizationMode="Recycling" BorderThickness="0" Width="260" MaxHeight="400" Margin="10" Name="zonesOnOffStack" SelectionMode="Single">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Visibility="{Binding visible}" IsChecked="{Binding enabled}" Content="{Binding name}" />
<TextBlock Padding="5,-1,0,0" Loaded="loadDesc" Text="{Binding desc, StringFormat={}({0})}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListView>
答案 0 :(得分:0)
我看到这很旧了,所以可能没有帮助,但是我想指出几件事。
证明断开连接的项目是具有约束力的东西: https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Data/BindingExpressionBase.cs,71f3b982041c21ce
希望有帮助:)