C# 如何选择使用包裹面板作为listview的项目面板
我希望水平显示一个项目列表,并能够从该项目列表中进行选择-我尝试使用listview,并根据堆栈上的一些帖子更改项目模板,我有以下几点:C# 如何选择使用包裹面板作为listview的项目面板,c#,wpf,listview,wrappanel,C#,Wpf,Listview,Wrappanel,我希望水平显示一个项目列表,并能够从该项目列表中进行选择-我尝试使用listview,并根据堆栈上的一些帖子更改项目模板,我有以下几点: <ListView Height="Auto" HorizontalAlignment="Left" Margin="24,0,0,0" Name="MachinesListView" VerticalAlignment="Top" Width="1455" Background="#FFF0F0F0" ItemsSource="{Binding Mac
<ListView Height="Auto" HorizontalAlignment="Left" Margin="24,0,0,0" Name="MachinesListView" VerticalAlignment="Top" Width="1455" Background="#FFF0F0F0" ItemsSource="{Binding Machines}" BorderBrush="#FFF0F0F0" Grid.ColumnSpan="2" Grid.Row="2" SelectionChanged="MachinesListView_SelectionChanged">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="{Binding (FrameworkElement.ActualWidth),
RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
ItemWidth="{Binding (ListView.View).ItemWidth,
RelativeSource={RelativeSource AncestorType=ListView}}"
MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}"
ItemHeight="{Binding (ListView.View).ItemHeight,
RelativeSource={RelativeSource AncestorType=ListView}}" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Height" Value="175"/>
<Setter Property="Width" Value="275"/>
<Setter Property="Margin" Value="5,5,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border BorderBrush="Gray" BorderThickness="2" CornerRadius="10">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Machine.MachineId}" TextAlignment="Center" Width="Auto" Foreground="#FF639A70" FontSize="19"/>
<TextBlock Text="{Binding Machine.Name}" TextAlignment="Center" Width="Auto" Foreground="Gray" FontSize="15" />
<Image Source="/URM;component/Images/slot_machine-512.png" Height="60" Width="60" />
<TextBlock Text="{Binding Machine.Description}" TextAlignment="Center" Width="Auto" Foreground="Gray" FontSize="15" Margin="0, 5, 0, 0"/>
<TextBlock TextAlignment="Center" Width="Auto" Foreground="Gray" FontSize="15" Margin="0, 5, 0, 0">
<Run Text ="Actual: "/>
<Run Text ="{Binding Actual, StringFormat=' {0:c}'}"/>
</TextBlock>
<TextBlock TextAlignment="Center" Width="Auto" Foreground="Gray" FontSize="15" Margin="0, 5, 0, 0">
<Run Text ="OverShort: "/>
<Run Text ="{Binding OverShort, StringFormat=' {0:c}'}"/>
</TextBlock>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
这工作得很好…我从中获得了我想要的整体外观和感觉,但仅选择第一项会导致selectionchanged事件触发
我考虑将这些项改为按钮,但我认为可能有一种更合适的方法来处理这个问题。在ControlTemplate的最外层边框上将背景设置为透明
<Border BorderBrush="Gray" Background="Transparent"
BorderThickness="2" CornerRadius="10">
默认情况下,{x:Null}对鼠标事件没有响应
只有在单击图像时,您才会看到该项被选中
或模板中存在的任何控件,但单击空白区域时
在边框内,selectionChange事件不会激发,因为它是
对鼠标事件无响应,特别是在您的情况下鼠标单击。将背景设置为透明将使其对所有鼠标事件作出响应
有关更多详细信息,请参阅本页-。问题出在哪里?您不能从列表中选择任何项目吗?它只选择第一个可用项目?我只能选择第一个项目。或者我应该说selectionchanged事件仅在您选择第一项时触发。啊-这是要添加的内容-当使用selectionchanged事件时,ListView的选定项从未更改-但是SelectionChangedEventArgs在其AddItem数组中具有数据选定项datacontext。