【发布时间】:2018-07-05 05:52:45
【问题描述】:
我在我的 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 属性始终为空。
我错过了什么吗?
【问题讨论】:
-
任何绑定错误?
-
否,但属性为空:(
-
@Redplane 在输出窗口中查找任何
System.Windows.Data Error:错误。可悲的是,默认情况下绑定错误不会引发异常... -
BindingExpression:Path=Name;数据项=空;目标元素是“RedirectToUriCommandArgument”(HashCode=20031746);目标属性是“Uri”(类型“字符串”)
-
您运行的代码是否与您发布的代码不同?您不会在任何地方绑定到“Uri”。但回到为什么
Page为空的问题:我的猜测是你不能仅仅从DependencyObject绑定到另一个元素,因为DependencyObject既不在逻辑树中也不在视觉树中。跨度>
标签: wpf mvvm mvvm-light