传递UIElement到CommandParameter

3

我在我的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 属性始终为 null。
我有遗漏吗?

有任何绑定错误吗? - Babbillumpa
不,但是该属性为空值 :( - Redplane
@Redplane,请在输出窗口查找任何System.Windows.Data Error:错误。遗憾的是,默认情况下绑定错误不会抛出异常... - Freggar
BindingExpression:Path=Name; DataItem=null; 目标元素为'RedirectToUriCommandArgument' (HashCode=20031746); 目标属性为'Uri' (类型为'String')。 - Redplane
你是否运行了除你发布的代码之外的其他代码?你没有在任何地方绑定到“Uri”。但是回到“Page”为空的问题:我猜测你不能仅仅从“DependencyObject”绑定到另一个元素,因为“DependencyObject”既不在逻辑树中也不在视觉树中。 - Freggar
你尝试过为你的“Page”绑定放置一个转换器吗?只需要一个简单的转换器,使其返回你传递的内容,并在“converter”内部设置断点。 - XAMlMAX
1个回答

1
我认为问题在于,当绑定被初始化时,UIElement尚未创建(为null)。之后,绑定未被通知对象已创建。
绑定属性更容易,对象必须实现INotifyPropertyChanged或DependencyObject来处理依赖属性。
要解决您的问题,可以为绑定设置延迟,例如1000毫秒,然后它将起作用。这是否是正确的方式令人怀疑。
<commandArgument:RedirectToUriCommandArgument Page="{Binding ElementName=PageFrame, Delay=1000}" Uri="MainView.xaml"></commandArgument:RedirectToUriCommandArgument>

正确的方法是将绑定的源设置为UI元素:
<commandArgument:RedirectToUriCommandArgument Page="{Binding Source={x:reference PageFrame}}" Uri="MainView.xaml"></commandArgument:RedirectToUriCommandArgument>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接