当我在列表框中选择项目时,下面的代码不起作用,你碰巧知道为什么吗?
<ListBox BorderBrush="Transparent" Background="Transparent" Name="listbox" HorizontalAlignment="Center" VerticalAlignment="Center" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectionChanged="selection_changed">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Button Height="90" Width="150" Template="{StaticResource cbutton}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
模板cbutton看起来像这样
<ControlTemplate x:Key="cbutton" TargetType="Button">
<Border CornerRadius="3" BorderThickness="3.5" BorderBrush="White">
<Border.Background>
<LinearGradientBrush EndPoint="1,1" StartPoint="0,0">
<GradientStop Color="DarkOrange" Offset="0.1"/>
<GradientStop Color="Orange" Offset="0.85"/>
</LinearGradientBrush>
</Border.Background>
<TextBlock FontWeight="ExtraBold" Foreground="White" TextAlignment="Center" TextWrapping="Wrap" FontSize="15" Text="{Binding name}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Border>
</ControlTemplate>
答案 0 :(得分:1)
SelectionChanged
事件未被触发,因为按钮是捕获鼠标点击的控件,而不是ListBox
。
您可以将事件处理程序设置为按钮的单击事件。
<Button Height="90" Width="150" Click="myClickEventHandler"/>
无论如何,我建议您使用MVVM,而不是Code-Behind事件处理程序。
您可以设置一个命令,当按钮单击时将触发该命令并发送命令按钮的内容,例如
<Button Name="myButton" Height="90" Width="150" Template="{StaticResource cbutton}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding DoSomething}" CommandParameter="{Binding ElementName=myButton, Path=Content}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
视图模型
DoSomething = new DelegateCommand<object>(content=>
{
// Do whatever you want
});
如果你不熟悉MVVM,它需要一些时间来学习它,但它绝对值得:)
答案 1 :(得分:1)
您可以在ListBoxItem上添加PreviewMouseDown事件处理程序:
# If you are using an older version of bash, you'll need
# to set the extglob option first.
# shopt -s extglob
if [[ ${ALLVERSION[i]} == /dir/test_*(!(_))_*(!(_)) ]]; then
unset ALLVERSION[i]
fi