绑定窗口高度为wpf

时间:2015-05-13 06:47:51

标签: wpf xaml data-binding inotifypropertychanged

我有一个类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会被调用,但窗口高度保持不变。为什么会发生这种情况以及如何解决?

1 个答案:

答案 0 :(得分:0)

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 属性应该可以正常工作