首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将UIElement传递给CommandParameter

将UIElement传递给CommandParameter
EN

Stack Overflow用户
提问于 2018-07-05 05:52:45
回答 1查看 833关注 0票数 3

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

我创建了一个类RedirectToUriCommandArgument.cs

代码语言:javascript
复制
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文件中,我使用了:

代码语言:javascript
复制
<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属性始终为空。

我有遗漏什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-05 08:30:20

因此,我认为问题在于,在初始化绑定时,没有创建UIElement (Null)。在未通知绑定之后,将创建对象。

绑定到属性很容易,对象必须实现INotifyPropertyChangedDependencyObject,注意相关性属性。

为了解决您的问题,您可以为Binding设置一个Binding,比如1000 it,然后它就可以工作了。这是否是一条正确的道路是值得怀疑的。

代码语言:javascript
复制
<commandArgument:RedirectToUriCommandArgument Page="{Binding ElementName=PageFrame, Delay=1000}" Uri="MainView.xaml"></commandArgument:RedirectToUriCommandArgument>

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

代码语言:javascript
复制
<commandArgument:RedirectToUriCommandArgument Page="{Binding Source={x:reference PageFrame}}" Uri="MainView.xaml"></commandArgument:RedirectToUriCommandArgument>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51184181

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档