-2

我有一个 WPF 项目,在其中我有一个Resource Dictionary存储我的样式的地方。在这本词典中,我有一个ListBoxItem Template看起来像这样的。

    <DataTemplate x:Key="GenreStyle">
    <Grid Width="150" Height="150" Background="Transparent">
        <Image x:Name="GenreImage" Tag="{Binding Genre}" RenderOptions.BitmapScalingMode="HighQuality" Source="{Binding GenreSource}" Width="140" Height="140" MouseEnter="GenreImage_MouseEnter" MouseLeave="GenreImage_MouseLeave" MouseLeftButtonDown="GenreImage_MouseLeftButtonDown" MouseLeftButtonUp="GenreImage_MouseLeftButtonUp"/>
    </Grid>
</DataTemplate>

现在我想做的是,Tag当我点击每个项目时阅读图像。我现在做的是这样的:

private void StationsListLB_PreviewMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    var item = ItemsControl.ContainerFromElement(StationsListLB, e.OriginalSource as DependencyObject) as ListBoxItem;
    if (item != null)
    {
       MessageBox.Show(??????);
    }
}
4

1 回答 1

0

好的,想通了。我只需要捕获System.Windows.Input.MouseButtonEventArgs. 然后非常简单:

private void StationsListLB_PreviewMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    var item = ItemsControl.ContainerFromElement(StationsListLB, e.OriginalSource as DependencyObject) as ListBoxItem;
    if (item != null)
    {
        var opa = e.OriginalSource as System.Windows.Controls.Image;
        MessageBox.Show(opa.Tag.ToString());
    }
}
于 2013-08-06T13:10:07.613 回答