MVVM列表框DataTemplate SelectedItem弹出窗口

时间:2019-03-10 12:54:50

标签: mvvm popup

我有一个问题,需要帮助。 右键单击列表框SelectedItem打开弹出窗口,如何控制弹出显示的位置!

鼠标离开了selecteditem,但是selectedItem被选中了,我不想在鼠标的当前位置显示它,如何控制

谢谢!      `                                                                                                                                                                                    

<Grid>
    <ListBox x:Name="listbox" SelectedItem="{Binding SelectObj}"  ItemTemplate="{DynamicResource ItemTemplate}" ItemsSource="{Binding ListPersion}">
        <Event:Interaction.Triggers>
            <Event:EventTrigger EventName="PreviewMouseRightButtonUp">
                <Event:InvokeCommandAction Command="{Binding RightPopupEdit}"  CommandParameter="{Binding ElementName=listbox, Path=SelectedItem}" />
            </Event:EventTrigger>
        </Event:Interaction.Triggers>
    </ListBox>

    <Popup IsOpen="{Binding Isopen}"
             StaysOpen="False" AllowsTransparency="True" PopupAnimation="Slide" 
           PlacementTarget="{Binding ElementName=listbox,Path=SelectedItem}" Placement="MousePoint">
        <Border Width="110" BorderThickness="1" BorderBrush="Red" Height="60" Background="White">
            <StackPanel Orientation="Vertical">
                <TextBlock Text="ID"/>
                <TextBlock Text="{Binding SelectObj.ID}"/>
                <TextBlock Text="Name"/>
                <TextBlock Text="{Binding SelectObj.Name}"/>
            </StackPanel>
        </Border>
    </Popup>
</Grid>

`

enter code here

ViewModel代码

公共类MainViewModel:BindableBase     {         私人Person myVar;

    public Person SelectObj
    {
        get { return myVar; }
        set {SetProperty(ref myVar , value); }
    }


    private ObservableCollection<Person> listPersion;
    public ObservableCollection<Person> ListPersion
    {
        get { return listPersion; }
        set {SetProperty(ref listPersion , value); }
    }

    public MainViewModel()
    {
        RightPopupEdit = new DelegateCommand<Person>(Event_PopupEdit);
        ListPersion = new ObservableCollection<Person>
        {
            new Person() { ID = 1, Name = "Jon" },
            new Person() { ID = 2, Name = "Jon1" },
            new Person() { ID = 3, Name = "Jon2" },
            new Person() { ID = 4, Name = "Jon3" },
            new Person() { ID = 5, Name = "Jon4" },
            new Person() { ID = 6, Name = "Jon5" }
        };
    }

    private void Event_PopupEdit(Person obj)
    {
        Isopen = true;
    }

    private bool isopen;

    public bool Isopen
    {
        get { return isopen; }
        set {SetProperty(ref isopen , value); }
    }

    public ICommand RightPopupEdit { get; set; }
}

public class Person
{
    public int ID { get; set; }
    public string Name { get; set; }

}
enter code here

0 个答案:

没有答案
相关问题