WPF Mvvm模式下 Popup控件的显示问题

chezeqiang0317 2015-07-20 07:19:55
我有这么一个问题困惑了我很久了,我的程序想要一个这样的效果
一个列表,双击列表的项可以弹出一个弹出层.
所以我写了一个ContactVm类 .里边有一个IsOpenPopup的通知属性。把这个属性binding在前台Popup控件的IsOpen属性上
然后双击列表项的时候可以把IsOpenPopup这个属性赋值为True,这样就可以打开弹出层。
但事实是双击之后IsOpenPopup这个属性的确是被赋值成true了,但是在Popup打开后不知道为什么IsOpenPopup马上又被赋值为false。也就是说双击后Popup闪了一下就没有了。找了好久不知道是什么原因.希望以前有过同样问题的朋友可以给我一个解答.

不知道我有没有表达清楚我的意思,还是直接贴代码吧.
ContactViewModel

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);
}
}

}
}



MainWindowViewModel

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;
}
}

}
}


MainWindow

<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>



我把代码上传到网盘了.直接下载源码更方便 谢谢大家!
http://pan.baidu.com/s/1mg3TzJU
...全文
823 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
thisishexiao 2015-08-06
  • 打赏
  • 举报
回复
引用 4 楼 chezeqiang0317 的回复:
这个看似简单的问题我到现在还没有做出来. 来人救我啊! 或者提供其他解决方式也可以啊
我说的你试了没?不要在XAML里吧StyasOpen设置为False,而是在代码里控制Popup的开和关
慧眼识狗熊 2015-08-06
  • 打赏
  • 举报
回复
上面说法不准确,不过确实是一个解决方案。 你说了button之后我想起来了,MouseLeftButtonDown这个事件的问题,你用up事件就没问题了。我之前遇到过keydown无法捕获按钮,只有keyup才行的事情。具体什么原因,你研究出来之后告诉我一下,哈哈。
chezeqiang0317 2015-08-05
  • 打赏
  • 举报
回复

<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就只闪一下就关了?
chezeqiang0317 2015-08-05
  • 打赏
  • 举报
回复
引用 5 楼 small21 的回复:
本来就是这样的。 你应该设置StaysOpen="True",然后单击改为true。再单击或者当前的失去焦点就设置为false。 你这个代码如果实现了你要的功能,单击一下就一直在,那你什么时候消失呢。
StaysOpen="False" 不就是在Popup没有焦点的时候把popup隐藏的吗?
慧眼识狗熊 2015-08-05
  • 打赏
  • 举报
回复
本来就是这样的。 你应该设置StaysOpen="True",然后单击改为true。再单击或者当前的失去焦点就设置为false。 你这个代码如果实现了你要的功能,单击一下就一直在,那你什么时候消失呢。
chezeqiang0317 2015-08-04
  • 打赏
  • 举报
回复
这个看似简单的问题我到现在还没有做出来. 来人救我啊! 或者提供其他解决方式也可以啊
thisishexiao 2015-07-22
  • 打赏
  • 举报
回复
估计是StaysOpen="False"搞的鬼 另外回复2楼,这里普通.Net属性就行吧,没必要用DependencyProperty吧。
exception92 2015-07-21
  • 打赏
  • 举报
回复
IsOpenPopup 这样声明 只是 简单的 。net 属性,你应该声明成 “依赖项属性”绑定。
chezeqiang0317 2015-07-20
  • 打赏
  • 举报
回复
我把代码上传到网盘了.直接下载源码更方便 谢谢大家! http://pan.baidu.com/s/1mg3TzJU

8,734

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧