如何使用wrappanel处理选择事件

时间:2016-01-28 00:27:00

标签: c# wpf mvvm

我目前正在使用C#WPF项目,我会在几行中显示图像(蓝头)

enter image description here

问题是我无法选择任何这些项目,我使用MVVM模式,因此后面的代码必须尽可能轻,我必须在xaml文件中执行操作。

所以我希望能够通过点击它们来选择图像,我已经尝试过像#34; IsMouseOver"这样的事件。但我只能改变整个包裹而不是单个物品。

以下是我使用的代码:

<Grid Grid.Row="1" Height="Auto">
        <Grid.Background>
            <LinearGradientBrush>
                <LinearGradientBrush.GradientStops>
                    <GradientStop Color="#252525" />
                </LinearGradientBrush.GradientStops>
            </LinearGradientBrush>
        </Grid.Background>
        <ItemsControl Background="Transparent" Foreground="AntiqueWhite" BorderBrush="Transparent"
                 HorizontalContentAlignment="Stretch" ItemsSource="{Binding Source={x:Static Context:Session.CurrentSession}, Path=CurrentProject.Models}">
            <ItemsControl.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Delete" Command="{Binding DeleteModel3DCommand}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}"/>
                </ContextMenu>                    
            </ItemsControl.ContextMenu>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding SelectModel3DCommand}" CommandParameter="{Binding Path=SelectedItem, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Image Source="/McKineap;component/Resources/Images/logo-mckineap.png" Height="100"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel/>   
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </Grid>

感谢您的时间,我会就您在报道中定义选择事件的正确方法提出任何建议!

2 个答案:

答案 0 :(得分:6)

ItemsControl项目并不是可选择的,这就是为什么缺少选择事件和选择功能的原因,更具体地说ItemsControl不会继承{{1}另一方面,允许这样做的类ListBoxListView

Selector更改为ItemsControl,您应该能够实施选择:

ListView

修改 不要忘记 <ListView SelectionMode="Single" Background="Transparent" Foreground="AntiqueWhite" BorderBrush="Transparent" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Items}"> 按顺序禁用Horizo​​ntalScrollBar以使ListView正常工作

WrapPanel

答案 1 :(得分:1)

我尝试以相同的方式,但使用ListBox而不是ListView,它适用于我。

    <Grid Grid.Row="1" Height="Auto">
        <Grid.Background>
            <LinearGradientBrush>
                <LinearGradientBrush.GradientStops>
                    <GradientStop Color="#252525" />
                </LinearGradientBrush.GradientStops>
            </LinearGradientBrush>
        </Grid.Background>
        <ListBox Name="ModelsListBox" Background="Transparent" Foreground="AntiqueWhite" BorderBrush="Transparent" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                 HorizontalContentAlignment="Stretch" ItemsSource="{Binding Source={x:Static Context:Session.CurrentSession}, Path=CurrentProject.Models}">
            <ListBox.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Delete" Command="{Binding DeleteModel3DCommand}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}"/>
                    <MenuItem Header="Rename"/>
                </ContextMenu>
            </ListBox.ContextMenu>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding SelectModel3DCommand}" CommandParameter="{Binding Path=SelectedItem, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="0,0,5,0" HorizontalAlignment="Left">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Image Source="/McKineap;component/Resources/Images/logo-mckineap.png"  Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Bottom" Height="55" Width="50"/>
                        <ListBoxItem Grid.Column="1" Content="{Binding NameWithoutExtension}" HorizontalAlignment="Stretch" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>
    </Grid>