MVVM弹出问题

时间:2010-12-17 02:57:36

标签: wpf mvvm popup

我是WPF的新手,甚至是MVVM的新手。我已经设置了一个简单的应用程序来尝试理解WPPF。在我的申请中,我有以下内容:

MainWindow.xaml - >我有一个按钮。我想如果鼠标翻过这个按钮我想要一个简单的弹出窗口显示。

MainWindowViewModel - >我创建了一个属性(popupstatus),我将其用作mouseover事件处理程序的触发器。

MyPopUp.xaml - >在此视图中,我已设置弹出窗口详细信息。我想使用MainWindowViewModel中的属性来触发它是否应该显示。

但即便在这之后我遇到了两个问题:一个我似乎无法使用动画来创建我在MainWindow.xaml文件中的MainWindowViewModel中创建的属性。我得到一个“无法解析属性路径'PopUpStatus'中的所有属性引用。” 。另一个是我无法在弹出的代码中绑定到这个问题。

最终我想要一个用于弹出窗口的viewmodel,它会做一些更高级的东西。

感谢您的帮助:)

MainWindow.xaml:     
        

        <Button.Triggers>
            <EventTrigger RoutedEvent="MouseEnter">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <BooleanAnimationUsingKeyFrames Storyboard.Target="{Binding .}" Storyboard.TargetProperty="PopUpStatus">
                                <DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:2"/>
                            </BooleanAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
            <EventTrigger RoutedEvent="MouseLeave">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <BooleanAnimationUsingKeyFrames Storyboard.TargetName="FiberPopUp" Storyboard.TargetProperty="IsOpen">
                                <DiscreteBooleanKeyFrame Value="False" KeyTime="0:0:2"/>
                            </BooleanAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Button.Triggers>
    </Button>
</Grid>

MainWindowViewModel:  private bool _PopUpStatus = false;         public MainWindowViewModel()         {         }

    public bool PopUpStatus
    {
        get
        {
            return _PopUpStatus;
        }
        set
        {
            _PopUpStatus = value;
            RaisePropertyChanged("PopUpStatus");
        }

    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(name));
    }

myPopUp.xaml                                     - &GT;         

        <StackPanel Width="auto"  Height="auto" >
            <TextBlock Background="White" Foreground="Black" Text="This is a test."/>
            <Button Content="ClosePopUp" Click="PopUpClose" />

        </StackPanel>

    </Popup>



</Grid>

1 个答案:

答案 0 :(得分:0)

你必须让你的主窗口视图中的弹出窗口处于折叠状态,然后使用visualstatemanager你有几个条件可以让你改变那个视图的可见性,以及一个灰色的矩形......

这就是我如何为我的应用程序完成它。我实际上将弹出窗口用作服务......

 <Grid wpfi:VisualStateAssistant.CurrentVisualState="{Binding Path=CurrentVisualState}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="640" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="480" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="ActiveFormStateGroup">
                <VisualStateGroup.Transitions>

                </VisualStateGroup.Transitions>
                <VisualState x:Name="Searching">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="SearchHeaderView">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Visible}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="SearchNavigationView">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Visible}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="SearchResultsView">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Visible}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="DoctorsModuleShell">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Collapsed}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="DoctorsModuleNavigationView">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Collapsed}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>

                </VisualState>
                <VisualState x:Name="DoctorEdit">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="SearchHeaderView">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Collapsed}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="SearchNavigationView">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Collapsed}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="SearchResultsView">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Collapsed}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="DoctorsModuleShell">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Visible}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="DoctorsModuleNavigationView">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Visible}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>

                </VisualState>

            </VisualStateGroup>
            <VisualStateGroup x:Name="VisualAlertsStateGroup">
                <VisualStateGroup.Transitions>

                </VisualStateGroup.Transitions>
                <VisualState x:Name="DialogShowing">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="rectangle">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Visible}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="DialogNotShowing">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="rectangle">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Collapsed}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="IsWaiting">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="rectangle">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Visible}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="WaitControl">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Visible}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="IsNotWaiting">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="rectangle">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Collapsed}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="WaitControl">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Collapsed}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

            </VisualStateGroup>
            <VisualStateGroup x:Name="DialogFormsStateGroup">
                <VisualStateGroup.Transitions>

                </VisualStateGroup.Transitions>
                <VisualState x:Name="ShowContactInfoEdit">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="rectangle">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Visible}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="ContactInfoDatagridView">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Visible}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>

                </VisualState>
                <VisualState x:Name="HideContactInfoEdit">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="rectangle">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Collapsed}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="ContactInfoDatagridView">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Collapsed}" />

                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>

                </VisualState>
                <VisualState x:Name="ShowDoctorTaxonomyEdit">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="rectangle">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Visible}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="DoctorsTaxonomyEditView">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Visible}" />

                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="HideDoctorTaxonomyEdit">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="rectangle">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Collapsed}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="DoctorsTaxonomyEditView">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Collapsed}" />

                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

            </VisualStateGroup>
            <VisualStateGroup x:Name="QuickEditViewStateGroup">
                <VisualStateGroup.Transitions>
                </VisualStateGroup.Transitions>

                <VisualState x:Name="ShowQuickEditDoctorOfficeView">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="rectangle">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Visible}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="QuickEditDoctorOfficeView">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Visible}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>

                </VisualState>

                <VisualState x:Name="HideQuickEditDoctorOfficeView">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="rectangle">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Collapsed}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                       Storyboard.TargetName="QuickEditDoctorOfficeView">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{x:Static Visibility.Collapsed}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>

                </VisualState>

            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>

        <DockPanel Grid.Column="0"
                   Grid.Row="0"
                   Grid.ColumnSpan="3"
                   Grid.RowSpan="3">

            <!--TOP-->
            <Grid ShowGridLines="False"
                  DockPanel.Dock="Top">

                <views:SearchHeaderView x:Name="SearchHeaderView"
                                        Margin="0,0,0,5" />

            </Grid>
            <!--BOTTOM-->
            <Grid ShowGridLines="false"
                  DockPanel.Dock="Bottom">

                <views:SearchNavigationView x:Name="SearchNavigationView" />
                <views:DoctorsModuleNavigationView  x:Name="DoctorsModuleNavigationView"
                                                    Visibility="Collapsed" />

            </Grid>

            <!--FILL-->
            <Grid ShowGridLines="False">

                <views:SearchResultsShell x:Name="SearchResultsView">
                    <views:SearchResultsShell.DataContext>
                        <vm:SearchResultsViewModel />
                    </views:SearchResultsShell.DataContext>
                </views:SearchResultsShell>
                <views:DoctorsModuleShell x:Name="DoctorsModuleShell"
                                          Visibility="Collapsed" />
            </Grid>

        </DockPanel>
        <Rectangle x:Name="rectangle"
                   RadiusX="2"
                   RadiusY="2"
                   Grid.Column="0"
                   Grid.Row="0"
                   Grid.ColumnSpan="3"
                   Grid.RowSpan="3"
                   Fill="{StaticResource modalFormHitTestRectangleBrush}"
                   IsHitTestVisible="True"
                   Visibility="Collapsed" />

        <controls:WaitingControl x:Name="WaitControl"
                                 Width="100"
                                 Height="100"
                                 Visibility="Collapsed"
                                 Grid.Column="1"
                                 Grid.Row="1" />

        <views:ContactInfoDatagridView x:Name="ContactInfoDatagridView"
                                       Grid.Column="1"
                                       Grid.Row="1"
                                       Visibility="Collapsed" />
        <views:DoctorsTaxonomyEditView x:Name="DoctorsTaxonomyEditView"
                                       Grid.Column="1"
                                       Grid.Row="1"
                                       Visibility="Collapsed" />

        <views:QuickEditDoctorOfficeView x:Name="QuickEditDoctorOfficeView"
                                         Grid.Column="1"
                                         Grid.Row="1"
                                         Visibility="Collapsed" />
    </Grid>

它看起来比实际上更令人困惑...基本上你绑定到主视图模型中的visualstate,然后根据它的值打开和关闭visualstates。