【发布时间】:2014-03-25 03:49:11
【问题描述】:
我有一个用户列表框,我想从中获取所选项目。我使用了 selecteditem 但它总是返回零。 这是我的列表框 xaml 代码:
<ListBox Name="_imageList" Margin="10,10,10,0" IsSynchronizedWithCurrentItem="True" ScrollViewer.HorizontalScrollBarVisibility="Visible" VerticalAlignment="Top" Height="250" BorderThickness="0" SelectionChanged="List_clicked">
<!--<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<EventSetter Event="MouseLeftButtonDown" Handler="ListBoxItem_MouseLeftButtonDown"/>
</Style>
</ListBox.ItemContainerStyle>-->
<ListBox.ItemTemplate>
<DataTemplate DataType="Enfant">
<Border CornerRadius="30">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Width="50" Height="80" Click="btn_click">
<Button.Template>
<ControlTemplate>
<Image x:Name="image" Source="{Binding avatar}"/>
</ControlTemplate>
</Button.Template>
</Button>
<TextBlock Grid.Row="1" x:Name="nom" Text="{Binding prenom}" VerticalAlignment="Center"/>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这是背后的代码:
private void btn_click(object sender, RoutedEventArgs e)
{
if (OnKidClick != null)
{
kid = new Enfant();
OnKidClick(this, new RoutedEventArgs());
var item = _imageList.SelectedItem;
}
}
【问题讨论】:
-
据我了解,@RohitVats 之前的
answer对您有所帮助。也许你应该接受它? -
是的,它有帮助,而且我没有使用 MVVM,我没有找到可以帮助我的结构良好的教程
-
你正在使用
Button他建议你的东西:Use Button in place of Image and override template of Button to give it an Image look, so that you can have clickable image.,与 MVVM 无关。