将UIElement传递给CommandParameter [英] Pass UIElement to CommandParameter
问题描述
我在WPF应用程序中使用MVVM Light.
I'm using MVVM Light in my WPF application.
我创建了一个类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
属性始终为空.
我错过了什么吗?
推荐答案
所以我认为问题是,在初始化绑定后,不会创建UIElement(空).之后,不通知绑定,即创建对象.
So I think the problem is, that as binding been initialized the UIElement is not created(null). Afterwords the binding is not notified, that the object is created.
绑定到属性更容易,对象必须实现INotifyPropertyChanged
或DependencyObject
要注意依赖属性.
Binding to the properties is easyer the object must implement INotifyPropertyChanged
or DependencyObject
take care about dependency properties.
要解决您的问题,您可以将Binding
设置为Delay
,例如设置为1000ms,那么它将起作用.这是否是正确的方法值得怀疑.
To solve your issue you could set a Delay
for Binding
, say to 1000ms, then it will work. It's doubtful whether it is a right way.
<commandArgument:RedirectToUriCommandArgument Page="{Binding ElementName=PageFrame, Delay=1000}" Uri="MainView.xaml"></commandArgument:RedirectToUriCommandArgument>
正确的方法是将绑定源设置为UIElement:
The right way would be just set binding's source to the UIElement:
<commandArgument:RedirectToUriCommandArgument Page="{Binding Source={x:reference PageFrame}}" Uri="MainView.xaml"></commandArgument:RedirectToUriCommandArgument>
这篇关于将UIElement传递给CommandParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!