将UIElement传递给CommandParameter

时间:2018-07-05 05:52:46

标签: wpf mvvm mvvm-light

我在WPF应用程序中使用MVVM Light。

我创建了一个类RedirectToUriCommandArgument.cs

public class RedirectToUriCommandArgument : DependencyObject
{
    #region Properties

    public static readonly DependencyProperty PageProperty =
        DependencyProperty.Register(nameof(Page), typeof(object), typeof(RedirectToUriCommandArgument), new UIPropertyMetadata(null));

    public object Page
    {
        get => (object)GetValue(PageProperty);
        set => SetValue(PageProperty, value);
    }

    public string Uri { get; set; }

    #endregion

    #region Methods



    #endregion
}

.xaml文件中,我使用了:

<Window x:Class="MainClient.Views.AppView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:v="clr-namespace:MainClient.Views"
        xmlns:vm="clr-namespace:MainClient.ViewModel"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:commandArgument="clr-namespace:MainClient.Models.CommandArguments"
        xmlns:local="clr-namespace:MainClient"

        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Height="350" Width="525">

    <Window.DataContext>
        <vm:AppViewModel x:Name="AppContext"></vm:AppViewModel>
    </Window.DataContext>

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

        <Frame NavigationUIVisibility="Hidden" x:Name="PageFrame">
            <Frame.Content>
                <Page Name="MainPage"></Page>
            </Frame.Content>
        </Frame>
        <StackPanel Grid.Row="1" Orientation="Horizontal">
            <Button>
                <Button.Content>
                    <TextBlock>Redirect to main view</TextBlock>
                </Button.Content>

                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <i:InvokeCommandAction Command="{Binding RedirectToViewRelayCommand}">
                            <i:InvokeCommandAction.CommandParameter>
                                <commandArgument:RedirectToUriCommandArgument Page="{Binding ElementName=PageFrame}" Uri="MainView.xaml"></commandArgument:RedirectToUriCommandArgument>
                            </i:InvokeCommandAction.CommandParameter>
                        </i:InvokeCommandAction>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>
        </StackPanel>
    </Grid>
</Window>

Page属性始终为空。

我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

所以我认为问题是,在绑定初始化后,UIElement未创建(空)。之后,不通知绑定,即创建对象。

绑定到属性更容易,对象必须实现INotifyPropertyChangedDependencyObject要注意依赖属性。

要解决您的问题,您可以将Delay的{​​{1}}设置为1000毫秒,那么它将起作用。这是否是正确的方法值得怀疑。

Binding

正确的方法是将绑定源设置为UIElement:

<commandArgument:RedirectToUriCommandArgument Page="{Binding ElementName=PageFrame, Delay=1000}" Uri="MainView.xaml"></commandArgument:RedirectToUriCommandArgument>