如何在WPF中使用棱镜打开新的模态对话框

时间:2018-08-02 10:35:45

标签: c# wpf xaml mvvm prism

我已经在Wpf MVVM中打开了模式对话框。但是我是棱镜的新手。我在MVVM中尝试了以下代码。现在工作正常。但是我想使用带色带棱镜的棱镜做同样的概念。在下面的代码中,我没有使用功能区窗口,而是使用了按钮而不是功能区窗口。请参考以下代码,

MainWindow.xaml

   <Window.Resources>
        <DataTemplate DataType="{x:Type vm:ModalDialogViewModel}">
            <view:ModalDialog />
        </DataTemplate>
    </Window.Resources> 

    <Grid>
        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Center">
            <Grid>               
                <Button
                    Width="150"
                    Height="25"
                    Margin="0,10,0,0"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    Command="{Binding Path=LoadViewCommand}"
                    Content="Show Modal Dialog" />                             
            </Grid>
        </Grid>

        <Grid Visibility="{Binding IsCloseModalWindow, Converter={StaticResource BooleanToVisibilityConverter}}">
            <Border Background="#90000000">
                <Border
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    Background="White"
                    BorderBrush="Transparent"
                    BorderThickness="0"
                    CornerRadius="0">
                    <Border.BitmapEffect>
                        <DropShadowBitmapEffect
                            Direction="270"
                            Opacity="0.5"
                            ShadowDepth="0.7"
                            Color="Black" />
                    </Border.BitmapEffect>

                    <ContentControl Content="{Binding Path=CurrentViewModel}" />
                </Border>
            </Border>
        </Grid>
    </Grid> 

MainWindowViewModel

public class MainWindowViewModel : ViewModelBase, ICloseWindow
    {
        private ICommand loadViewCommand;

        private ViewModelBase _currentViewModel;

        public ViewModelBase CurrentViewModel
        {
            get { return _currentViewModel; }
            set
            {
                _currentViewModel = value;
                this.OnPropertyChanged("CurrentViewModel");
            }
        }

        private bool isCloseModalWindow = false;

        public bool IsCloseModalWindow
        {
            get { return isCloseModalWindow; }
            set { isCloseModalWindow = value; OnPropertyChanged("IsCloseModalWindow"); }
        }


        public MainWindowViewModel()
        {

        }

        public ICommand LoadViewCommand => loadViewCommand ?? (loadViewCommand = new RelayCommand(showView, canShowView));

        private void showView(object obj)
        {
            IsCloseModalWindow = true;            

            CurrentViewModel = new ModalDialogViewModel(new ModalDialogModel() { Name = "New Modal Window" }, this);
        }

        private bool canShowView(object obj)
        {
            return true;
        }

        public void closeWindow()
        {
            IsCloseModalWindow = false;
        }
    }

ModalDialog

<Grid MinWidth="300" Background="White">

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Grid
            Grid.Row="0"
            MinHeight="30"
            Background="SkyBlue">

            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <Button                    
                    Grid.Column="1"
                    Width="40"
                    Height="25"
                    Content="X"
                    BorderThickness="0"
                    Background="Transparent"
                    Foreground="White"
                    Margin="0,0,5,0"
                    HorizontalAlignment="Right"
                    VerticalAlignment="Center"
                    Command="{Binding CancelCommand}"
                    CommandParameter="{Binding ElementName=modalDialog}"                    
                    ToolTip="Close" />
            </Grid>
        </Grid>

        <Grid Grid.Row="1" Margin="15">

            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Label
                Grid.Row="0"
                Grid.Column="0"
                Content="Name"
                Style="{StaticResource commonMargin}" />
            <Label
                Grid.Row="0"
                Grid.Column="1"
                Content=":"
                Style="{StaticResource commonMargin}" />
            <TextBox
                Grid.Row="0"
                Grid.Column="2"
                Width="100"   
                Text="{Binding Path=Name}"
                Style="{StaticResource commonTimerMargin}" />


        </Grid>

        <Grid
            Grid.Row="2"
            Margin="0,0,15,10"
            HorizontalAlignment="Right">

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <Button
                Grid.Column="0"
                Width="80"
                Height="30"
                Margin="0,0,10,0"
                Background="White"
                Command="{Binding CancelCommand}"
                CommandParameter="{Binding ElementName=modalDialog}"
                Content="CANCEL" />
            <Button
                Grid.Column="1"
                Width="80"
                Height="30"
                Background="SkyBlue"
                Command="{Binding ApplyCommand}"
                CommandParameter="{Binding ElementName=modalDialog}"
                Content="APPLY"
                Foreground="White" />

        </Grid>
    </Grid>

ModalDialogViewModel

    public class ModalDialogViewModel : ViewModelBase
    {
        private ICommand cancelCommand;
        public ModalDialogModel Model { get; private set; }
        private ICloseWindow _closeWindow;

        public ModalDialogViewModel(ModalDialogModel modalDialogModel, ICloseWindow closeWindow)
        {
            this.Model = modalDialogModel;
            this._closeWindow = closeWindow;           
        }

        public ICommand CancelCommand => cancelCommand ?? (cancelCommand = new RelayCommand(CloseWindow, CanCloseWindow));        

        private void CloseWindow(object obj)
        {
            _closeWindow.closeWindow();
        }

        private bool CanCloseWindow(object obj)
        {
            return true;
        }
    }

我的要求是,当用户单击功能区窗口按钮时,将打开模式对话框。外壳程序窗口是功能区窗口。我已经将HomeTab模块和许多其他模块添加为单独的类库。

1 个答案:

答案 0 :(得分:1)

您可以在.xaml中这样做:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:prism="http://prismlibrary.com/"

<i:Interaction.Triggers>
    <prism:InteractionRequestTrigger SourceObject="{Binding PopUpDialogActionBinding}">   '<-- Here is your action binding
        <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="False">
            <prism:PopupWindowAction.WindowStyle>
                <Style TargetType="Window">
                    <Setter Property="Icon" Value="IconPath"/>
                    <Setter Property="Height" Value="400"/>
                    <Setter Property="Width" Value="400"/>
                </Style>
            </prism:PopupWindowAction.WindowStyle>
            <prism:PopupWindowAction.WindowContent>
                <views:YourCustomView />   ' <--- Put your view into the dialog
            </prism:PopupWindowAction.WindowContent>
        </prism:PopupWindowAction>
    </prism:InteractionRequestTrigger>
</i:Interaction.Triggers>

<Grid>
...
</Grid>

我按照示例28进行操作:

https://github.com/PrismLibrary/Prism-Samples-Wpf