gpt4 book ai didi

wpf - 将命令绑定(bind)到 View 的 Loaded 事件

转载 作者:行者123 更新时间:2023-12-02 07:28:44 26 4
gpt4 key购买 nike

我试图让一个方法在 View 加载完成时运行。我尝试将命令绑定(bind)到 View 中的 Loaded 事件,但它无法运行。抛出的内部异常是

'Provide value on 'System.Windows.Data.Binding' threw an exception.' Line number '14' and line position '14'

<UserControl x:Class="Components.Map.MapView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:map="clr-namespace:Components.Map"
xmlns:controls="clr-namespace:Windows.Controls;assembly=Windows.Controls"
xmlns:ValidationRules="clr-namespace:Windows.Controls.ValidationRules;assembly=Windows.Controls"
xmlns:directGraphicsControl="clr-namespace:Windows.DirectGraphicsControl;assembly=Windows.DirectGraphicsControl"
xmlns:colorBar="clr-namespace:Components.Common.ColorBar;assembly=Components.Common"
xmlns:RefinedRibbonControls="clr-namespace:Components.Common.Controls.RefinedRibbonControls;assembly=Components.Common"
xmlns:UserControls="clr-namespace:Components.Common.UserControls;assembly=Components.Common"
xmlns:map1="clr-namespace:Models.Map;assembly=Models.Map"
xmlns:utilities="clr-namespace:Windows.Utilities;assembly=Windows.Utilities"
xmlns:system="clr-namespace:System;assembly=mscorlib"
Loaded="{Binding Path=MapControlViewModel.MapLoadedCommand}">

如何绑定(bind)到 View 的 Loaded 事件,以便在 View 加载完成后运行某些内容?

最佳答案

如果要将命令绑定(bind)到 Loaded 事件,则应使用“System.Windows.Interactivity”程序集。

<UserControl x:Class="Components.Map.MapView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:map="clr-namespace:Components.Map"
xmlns:controls="clr-namespace:Windows.Controls;assembly=Windows.Controls"
xmlns:ValidationRules="clr-namespace:Windows.Controls.ValidationRules;assembly=Windows.Controls"
xmlns:directGraphicsControl="clr-namespace:Windows.DirectGraphicsControl;assembly=Windows.DirectGraphicsControl"
xmlns:colorBar="clr-namespace:Components.Common.ColorBar;assembly=Components.Common"
xmlns:RefinedRibbonControls="clr-namespace:Components.Common.Controls.RefinedRibbonControls;assembly=Components.Common"
xmlns:UserControls="clr-namespace:Components.Common.UserControls;assembly=Components.Common"
xmlns:map1="clr-namespace:Models.Map;assembly=Models.Map"
xmlns:utilities="clr-namespace:Windows.Utilities;assembly=Windows.Utilities"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">

<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding LoadedCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>

</UserControl>

System.Windows.Interactivity.dll 位于 Microsoft Expression Blend 软件开发工具包 (SDK) ( download link ) 中,也可作为 NuGet package 提供。 .

关于wpf - 将命令绑定(bind)到 View 的 Loaded 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17933102/

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