【发布时间】:2012-06-04 13:48:48
【问题描述】:
我的一个网格单元中有一个带有超链接的列表框。当我单击列表框中的任何项目时,当前项目始终是列表框中的第一条记录,并且所选项目始终为空。这是我的WPF。我做错了什么?
<DataTemplate x:Key="CellTemplate.InputManagersList">
<StackPanel Orientation="Horizontal">
<ListBox ItemsSource="{Binding Path=InputManagers}" HorizontalAlignment="Left">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock>
<Hyperlink Name="inputManagerName"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.OpenInputManagerViewCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}">
<TextBlock Text="{Binding Path=Name}" />
</Hyperlink>
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Style>
<Style TargetType="ListBox">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="BorderThickness" Value="0"/>
</Style>
</ListBox.Style>
</ListBox>
</StackPanel>
</DataTemplate>
【问题讨论】:
-
你怎么知道选择了什么项目? ListBox 甚至没有名称。请将您的代码发布到检查所选项目的位置。
-
我的命令方法的参数是一个ListBox。所以我只是在我的代码中查看那个对象。 listBox.SelectedItem;
-
所以你不打算发布那个代码?
-
我不确定你想要什么代码。 C# 代码非常少。我检查了我的方法参数,正如您从上面的 xaml 中看到的那样,它就是 listBox 本身。然后我检查 listBox.SelectedItem。它是空的。这是 C# 代码的总和。
-
这确认您不会在获得空引用的地方发布代码。然而,问题是关于空引用。而且没有一个被接受的答案。