【问题标题】:Binding window height in wpfwpf中的绑定窗口高度
【发布时间】:2015-05-13 06:47:50
【问题描述】:

我有一个类 VirtualKeyboard,它扩展了 Window 类。 我有另一个类 - EnglishVirtualKeyboard,它从 VirtualKeyboard 类扩展而来。

这就是我在 EnglishVirtualKeyboard.xaml 中的内容:

<vk:VirtualKeyboard x:Class="Hurst.VirtualKeyboard.EnglishVirtualKeyboard"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
mc:Ignorable="d"
xmlns:vk="clr-namespace:Hurst.VirtualKeyboard"
xmlns:vm="clr-namespace:Hurst.VirtualKeyboard.ViewModels"
DataContext="{DynamicResource keyboardViewModel}"
Height="{Binding KeyboardWindowHeight}" Width="1280" d:DesignHeight="300" d:DesignWidth="710" 
x:Name="VK">

<vk:VirtualKeyboard.Resources>
    <ObjectDataProvider x:Key="keyboardViewModel" ObjectType="{x:Type vm:KeyboardViewModel}" />
</vk:VirtualKeyboard.Resources>

KeyboardWindowHeight 是 KeyboardViewModel 类中的一个属性。

当我点击键盘上的一个按钮时,我希望改变窗口的高度。这是我的代码:

if (buttonPressed)
{
    KeyboardWindowHeight = 400;
}
else
{
    KeyboardWindowHeight = 485;
}
Notify("KeyboardWindowHeight");

这是 Notify 方法:

public void Notify([CallerMemberName] string propertyName = "")
    {
        this.VerifyProperty(propertyName);

        // Make a copy of the PropertyChanged event first, before testing it for null,
        // because another thread might change it between the two statements.
        var copyOfPropertyChangedEvent = PropertyChanged;

        if (copyOfPropertyChangedEvent != null)
        {
            // Get the cached event-arg.
            var args = GetPropertyChangedEventArgs(propertyName);
            copyOfPropertyChangedEvent(this, args);
        }

        this.AfterPropertyChanged(propertyName);
    }

    public event PropertyChangedEventHandler PropertyChanged;

此代码位于 ViewModel 类中,KeyboardViewModel 从该类扩展并实现 INotifyPropertyChanged 接口。

我的问题是这样的:

当我点击按钮时,KeyboardWindowHeight 被改变,Notify 被调用,但窗口高度保持不变。为什么会发生这种情况以及如何解决?

【问题讨论】:

  • 您是否尝试将 KeyboardWindowHeight 属性绑定到 MinHeight 和 MaxHeight 以及 Height?

标签: wpf xaml data-binding inotifypropertychanged


【解决方案1】:
public bool Button_clicked
    {
        get
        {
            return _button_clicked;
        }
        set
        {
            _button_clicked = value;
            if (value)
                Win_height = 500;
            else
                Win_height = 300;
            onPropertyChanged("Button_clicked");
        }
    }

Win_height 属性绑定到 xaml 中的 Height 属性应该可以工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 2011-02-10
    • 2012-09-02
    • 2013-03-19
    相关资源
    最近更新 更多