8,734
社区成员
发帖
与我相关
我的任务
分享
using System;
using GalaSoft.MvvmLight;
namespace QuestionPopup.ViewModels
{
public class ContactVm : ViewModelBase
{
public string Name { get; set; }
private bool _IsOpenPopup;
public bool IsOpenPopup
{
get { return _IsOpenPopup; }
set
{
_IsOpenPopup = value;
RaisePropertyChanged(() => this.IsOpenPopup);
}
}
}
}
using System;
using System.Collections.Generic;
using GalaSoft.MvvmLight.Command;
namespace QuestionPopup.ViewModels
{
public class MainViewVm
{
public MainViewVm()
{
this.ContactList = new List<ContactVm>()
{
new ContactVm{ Name = "张三", IsOpenPopup = false },
new ContactVm{ Name = "李四", IsOpenPopup = false },
new ContactVm{ Name = "王五", IsOpenPopup = false },
new ContactVm{ Name = "赵六", IsOpenPopup = false },
};
}
public List<ContactVm> ContactList { get; set; }
private RelayCommand<ContactVm> _DoubleClickContactItem = null;
public RelayCommand<ContactVm> DoubleClickContactItem
{
get
{
if (_DoubleClickContactItem == null)
{
_DoubleClickContactItem = new RelayCommand<ContactVm>(item =>
{
item.IsOpenPopup = true;
});
}
return _DoubleClickContactItem;
}
}
}
}
<Window x:Class="QuestionPopup.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ItemsControl ItemsSource="{Binding ContactList}" HorizontalAlignment="Center">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Border x:Name="border" Width="100" Height="100" Margin="5" Background="Gray">
<TextBlock Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Border>
<Popup x:Name="popup" IsOpen="{Binding IsOpenPopup}" StaysOpen="False" Placement="Right" PlacementTarget="{Binding ElementName=border}">
<Border Width="200" Height="200" Background="Green"></Border>
</Popup>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding DataContext.DoubleClickContactItem, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}, Mode=FindAncestor}}"
CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Window>
<Button x:Name="asd" Width="100" Height="100" Click="asd_Click" />
<Popup x:Name="popup" StaysOpen="False" Placement="Right" PlacementTarget="{Binding ElementName=asd}">
<Border Width="200" Height="200" Background="Green"/>
</Popup>
private void asd_Click(object sender, RoutedEventArgs e)
{
popup.IsOpen = true;
}
<Border x:Name="asd" Width="100" Height="100" Background="Gray" MouseLeftButtonDown="Border_MouseLeftButtonDown" />
<Popup x:Name="popup" StaysOpen="False" Placement="Right" PlacementTarget="{Binding ElementName=asd}">
<Border Width="200" Height="200" Background="Green"/>
</Popup>
private void Border_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
popup.IsOpen = true;
}
为什么这里用Button的Click事件可以正常显示Popup
但用Border的MouseLeftButtonDown事件Popup就只闪一下就关了?