使用定位器模式将子窗口绑定到静态视图模型没有任何技巧。我的猜测是您的 DataContext 是错误的。
检查:确保在定位器类中定义了“AddAlert”属性。就像是:
private static AddAlertViewModel _AddAlertViewModel;
/// <summary>
/// Gets the ViewModelPropertyName property.
/// </summary>
public static AddAlertViewModel AddAlertViewModelStatic
{
get
{
if (_AddAlertViewModel == null)
{
CreateAddAlertViewModel();
}
return _AddAlertViewModel;
}
}
/// <summary>
/// THIS PROPERTY IS WHAT YOU NEED TO REFERENCE IN YOUR XAML
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This non-static member is needed for data binding purposes.")]
public AddAlertViewModel AddAlert
{
get
{
return AddAlertViewModelStatic;
}
}
当然,请确保您的视图模型定位器已在您的 App.xaml 文件中实例化:
<vm:MyModelLocator xmlns:vm="clr-namespace:MyAppNamespace" x:Key="Locator" />