PopUp绑定未正确完成

时间:2016-12-30 11:25:13

标签: c# wpf mvvm binding popup

我有一个listview,在鼠标进入特定列时,我尝试通过在MyAction2()函数中将isOpen设置为true来启动viewmodel类中的弹出窗口,当用户在listview列上输入鼠标时会调用该函数。 / p>

我观察到当鼠标进入该列时。它调用我的函数(ViewModel中的MyAction2()函数,参见下面的代码),但即使在MyAction2()中将isopen变量设置为true,set-get也是如此。绑定的方法是打开不被调用。现在我觉得绑定有问题。通常应该是正确的我觉得有些东西丢失但我不知道是什么。

My Xaml(包含teh opup和ListView中的列,在鼠标输入中调用ViewModel中名为MyAction2()的事件):

<Grid>
    <StackPanel>           
        <Popup Margin="10,10,0,13" Name="Popup1" IsOpen="{Binding PopUpLaunched,Mode=TwoWay}" Placement="Top" PopupAnimation="Fade" StaysOpen="True" HorizontalAlignment="Left" VerticalAlignment="Top" Width="194" Height="200" MinWidth="500" MinHeight="500">
            <StackPanel>
                <Border Background="Red">
                    <TextBlock Name="McTextBlock" Background="LightBlue"> This is popup text </TextBlock>
                </Border>
            </StackPanel>
        </Popup>
    </StackPanel>
</Grid>

ViewModel.cs

private bool popUpLaunched;
public bool PopUpLaunched {
    get {
        return popUpLaunched;
    } //Get set never gets called even after the popUpLaunched=true in the MyAction2() call
    set {
        popUpLaunched = value;
        OnPropertyChanged("PopUpLaunched");
    }
}

private void MyAction2(object param) //The function which gets called on mouse event but do not pop ups the popup
    {
        popUpLaunched = true;
    }

什么是错的,哪里出错了?

2 个答案:

答案 0 :(得分:0)

您应该设置 P opupLaunched 属性,而不是设置要调用的setter的popUpLaunched字段以及要引发的PropertyChanged事件:

private void MyAction2(object param)
{
    PopUpLaunched = true;
}

答案 1 :(得分:0)

为了实现这样的绑定,您可以将该属性设为Dependency属性,如此

public static readonly DependencyProperty PopUpLaunched = DependencyProperty.Register(
    "popUpLaunched", typeof(bool), typeof(MainPage), new PropertyMetadata(null));

        public bool popUpLaunched
        {
            get { return (bool)GetValue(PopUpLaunched); }
            set { SetValue(PopUpLaunched, value); }
        }

如果您不在MainPage上工作,请分别更改该typeof(MainPage)参数。并根据您的需要调整吸气剂和定型剂。

相关问题