微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WPF Prism 对话框窗口 - 绑定到 DialogWindow 的宽度和高度

如何解决WPF Prism 对话框窗口 - 绑定到 DialogWindow 的宽度和高度

我正在尝试保存 Prism WPF 对话框的宽度和高度,以便在下次使用该对话框时检索。

我有一个静态类正在被序列化为 xml 以保存属性,因此我尝试通过将其注入构造函数并将其链接到 WindowWidth 和 WindowHeight 属性并使用绑定到一个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,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 中绑定的那些。

解决方法

您需要像这样将 UserControl 的 DataContext 设置为自身:

<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"
 DataContext="{Binding RelativeSource={RelativeSource Self}}">

然后就写

<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}" />
            <Setter Property="Width" Value="{Binding WindowWidth}" />

        </Style>
,

事实证明,绑定到视图模型中的属性而不是问题更新中描述的代码背后。 我第一次尝试时它不起作用的原因是我注册了一个自定义对话框窗口,并且该窗口的高度和宽度在 xaml 中硬编码,因此它没有响应绑定。 我刚刚删除了硬编码值,现在绑定可以工作了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。