【发布时间】:2021-06-05 05:00:38
【问题描述】:
我正在尝试保存 Prism WPF 对话框的宽度和高度,以便在下次使用该对话框时检索。
我有一个被序列化为 xml 以保存属性的静态类,因此我尝试通过将其注入构造函数并将其链接到 WindowWidth 和 WindowHeight 属性并将其绑定到 UserControl 后面的代码中,并使用绑定到xaml 中的命名元素如下所示:
用户控件 xaml:
<UserControl
x:Class="MyNameSpace.Views.MyView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:behaviors="clr-namespace:Microdesk.BIMrxCommon.WpfUi.Behaviors;assembly=Microdesk.BIMrxCommon.WpfUi"
xmlns:core="clr-namespace:System;assembly=mscorlib"
xmlns:data="clr-namespace:System.Data;assembly=System.Data"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:mvvm="http://prismlibrary.com/"
x:Name="MyViewControl"
mvvm:ViewModelLocator.AutoWireViewModel="True">
<mvvm:Dialog.WindowStyle>
<Style TargetType="Window">
<Setter Property="mvvm:Dialog.WindowStartupLocation" Value="CenterScreen" />
<Setter Property="ResizeMode" Value="CanResizeWithGrip" />
<Setter Property="ShowInTaskbar" Value="False" />
<Setter Property="SizeToContent" Value="Manual" />
<Setter Property="Height" Value="{Binding WindowHeight, Mode=TwoWay, ElementName=MyViewControl, UpdateSourceTrigger=PropertyChanged}" />
<Setter Property="Width" Value="{Binding WindowWidth, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ElementName=MyViewControl}" />
</Style>
</mvvm:Dialog.WindowStyle>
....
后面的代码:
public partial class MyView : UserControl {
public IApplicationSettings ApplicationSettings { get; set; }\\Injected in ctor
public double WindowHeight
{
get => ApplicationSettings.MyViewHeight;
set => ApplicationSettings.MyViewHeight = value;
}
public double WindowWidth {
get => ApplicationSettings.MyViewWidth;
set => ApplicationSettings.MyViewWidth = value;
}
...
但是绑定没有找到命名的 UserControl,而是试图在 DialogWindow 对象中找到属性:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=MyViewControl'. BindingExpression:Path=WindowHeight; DataItem=null; target element is 'DialogWindow' (Name=''); target property is 'Height' (type 'Double')
首先不确定这是否是正确的方法,感谢任何帮助。
更新:
我尝试将 WindowWidth 和 WindowHeight 属性添加到 ViewModel 而不是后面的代码。输出窗口没有显示任何关于绑定的抱怨,所以应该没问题,但是,在运行时更改窗口大小不会在视图模型中设置这些属性,就像实际窗口的宽度和高度是与我在 xaml 中绑定的那些。
【问题讨论】:
标签: c# wpf dialog prism sizing