window坐标以及大小等属性做绑定后不能生效

分号 分号 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
    public class ObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    internal class MainWindowModel : ObservableObject
    {
        private double _locationX = 0;
        private double _locationY = 0;

        private double _width = 800;
        private double _height = 600;

        private string _title = "Test";

        public double LocationX
        {
            get { return _locationX; }
            set
            {
                _locationX = value;
                OnPropertyChanged();
            }
        }

        public double LocationY
        {
            get { return _locationY; }
            set { _locationY = value; OnPropertyChanged(); }
        }

        public double WindowWidth
        {
            get { return _width; }
            set { _width = value; OnPropertyChanged(); }
        }

        public double WindowHeight
        {
            get { return _height; }
            set
            {
                _height = value; OnPropertyChanged();
            }
        }

        public string MyTitle
        {
            get { return _title; }
            set { _title = value; OnPropertyChanged(); }
        }
    }

window的xaml代码:

<Window
    x:Class="WpfApp2.MainWindow"
    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:local="clr-namespace:WpfApp2"
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Name="myWindow"
    Title="{Binding MyTitle}"
    Width="{Binding WindowWidth, UpdateSourceTrigger=Explicit}"
    Height="{Binding WindowHeight}"
    d:DataContext="{d:DesignInstance Type=local:MainWindowModel}"
    Background="{DynamicResource MaterialDesignPaper}"
    Left="{Binding LocationX}"
    Top="{Binding LocationY}"
    mc:Ignorable="d">
...
</Window>

为什么Binding后窗口的位置、大小都没有按照我设置的值来变化,但那个Title有效果。

我也尝试在窗口显示之后,点击上面的按钮,来主动设置viewmodel,但是也没效果。

有谁知道这是什么情况吗?

...全文
评论 1
收藏 已收藏
分享
返回顶部
分号
WPF/Silverlight 8,757 成员 12,031 内容