gpt4 book ai didi

wpf - 将 UIElement 传递给 CommandParameter

转载 作者:行者123 更新时间:2023-12-04 21:24:12 26 4
gpt4 key购买 nike

我在我的 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。

我错过了什么吗?

最佳答案

所以我认为问题在于,绑定(bind)已初始化时未创建 UIElement(空)。之后绑定(bind)没有被通知,对象被创建。

绑定(bind)到属性更容易,对象必须实现 INotifyPropertyChangedDependencyObject 来处理依赖属性。

要解决您的问题,您可以为Binding 设置一个Delay,比如说1000ms,然后它就会起作用。这种方式是否正确值得怀疑。

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

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

<commandArgument:RedirectToUriCommandArgument Page="{Binding Source={x:reference PageFrame}}" Uri="MainView.xaml"></commandArgument:RedirectToUriCommandArgument>

关于wpf - 将 UIElement 传递给 CommandParameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51184181/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com