Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 组合框SelectionChanged事件处理程序中的InvalidCastException_Wpf_Autocad - Fatal编程技术网

Wpf 组合框SelectionChanged事件处理程序中的InvalidCastException

Wpf 组合框SelectionChanged事件处理程序中的InvalidCastException,wpf,autocad,Wpf,Autocad,此代码运行良好: private void Combobox1_Loaded(object sender, RoutedEventArgs e) { var combo = (ComboBox)sender; var pointGroupList = (List<PointGroup>)combo.ItemsSource; combo.ItemsSource = pointGroupList.Select(group => group.Name

此代码运行良好:

 private void Combobox1_Loaded(object sender, RoutedEventArgs e)
 {
     var combo = (ComboBox)sender;
     var pointGroupList = (List<PointGroup>)combo.ItemsSource;
     combo.ItemsSource = pointGroupList.Select(group => group.Name);
 }
private void Combobox1\u已加载(对象发送方、路由目标方)
{
变量组合=(组合框)发送方;
var pointGroupList=(List)combo.ItemsSource;
combo.ItemsSource=pointGroupList.Select(group=>group.Name);
}
但这个根本不起作用:

private void Combobox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var combo = (ComboBox)sender;
    var pointGroupList = (List<PointGroup>)combo.ItemsSource;
    textBlock1.Text = "num of points:" + pointGroupList.Find(group => group.Name == (string)combo.SelectedItem).PointsCount.ToString();
}
private void组合框1\u SelectionChanged(对象发送方,selectionchangedventargs e)
{
变量组合=(组合框)发送方;
var pointGroupList=(List)combo.ItemsSource;
textBlock1.Text=“点数:”+pointGroupList.Find(group=>group.Name==(string)combo.SelectedItem.pointscont.ToString();
}
以下是“我的输出”窗口中的消息:

System.InvalidCastException:无法将类型为“WhereSelectListIterator2[Autodesk.Civil.DatabaseServices.PointGroup,System.String]”的对象强制转换为类型为“System.Collections.Generic.List1[Autodesk.Civil.DatabaseServices.PointGroup]”。在D:\00 Materials\c3d\c\examples\ACAD\01 COGO Points\Window.xaml.cs中的_01cogo Points.ModalDialog\u 1.Combobox1\u SelectionChanged(对象发送方,SelectionChangedEventArgs e)处


任何帮助都将不胜感激。

您在加载的
事件中所做的事情非常奇怪。我不建议这样做,因为它会破坏您的绑定。如果您这样做的原因是
名称
属性显示在您的
组合框中
,那么您应该使用
数据模板
。大概是这样的:

<Window.Resources>
    <DataTemplate x:Key="pntGroupTemplate" DataType="{x:Type ac:PointGroup}">
        <TextBlock Text="{Binding Name}"/>
    </DataTemplate>
</Window.Resources>
xmlns:ac="clr-namespace:Autodesk.Civil.DatabaseServices;assembly=AeccDbMgd"
我没有民事诉讼,所以不确定这是否正确,但应该很接近。如果这条路径不太正确,Intellisense应该能够帮助您找到正确的路径

在你的组合框里

<ComboBox ItemTemplate="{StaticResource pntGroupTemplate}" ...  />
为此:

group => group.Name == (combo.SelectedItem as PointGroup)?.Name
您得到的异常是由于第二行引起的。当您在加载的事件中调用
Select
方法时,它返回
IEnumerable
,因此当您将
项资源
强制转换到
列表
时,所有内容都会以多种不同的方式横向移动:-)

您现在所做的另一个问题是,
SelectedItem
是一个
字符串
,没有
名称
属性


希望这有助于

错误System.InvalidCastException:无法将类型为“WhereSelectListIterator
2[Autodesk.Civil.DatabaseServices.PointGroup,System.String]”的对象强制转换为类型为“System.Collections.Generic.List
1[Autodesk.Civil.DatabaseServices.PointGroup]”。在D:\00 Materials\c3d\c\examples\ACAD\01 COGO Points\Window.xaml.cs:49行名称空间前缀ac未定义(错误消息)您需要将名称空间添加到窗口中,例如
xmlns:ac=“clr命名空间:Autodesk.Civil.DatabaseServices;assembly=AeccDbMgd“
。我没有Civil,因此不确定这是否正确,但它应该很接近。Intellisense应该能够帮助您找到正确的路径。已将其添加到答案中。
group => group.Name == (combo.SelectedItem as PointGroup)?.Name