Problems with binding to Window Height and Width

后端 未结 9 793
长发绾君心
长发绾君心 2020-12-05 13:46

I have some problems when I try to bind the height and width of a window to properties in my view model. Here is a small sample app to illustrate the problem. This is the co

9条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 14:19

    I am not sure about your specific implementation, but I just write an example that maybe could helpful.

    XAML

    
        
            
            
    
    

    Code

    Class MainWindow
    
        Public Shared ReadOnly WindowWidthProperty As DependencyProperty = _
                               DependencyProperty.Register("WindowWidth", _
                               GetType(Integer), GetType(MainWindow), _
                               New FrameworkPropertyMetadata(Nothing))
    
        Public Shared ReadOnly WindowHeightProperty As DependencyProperty = _
                             DependencyProperty.Register("WindowHeight", _
                             GetType(Integer), GetType(MainWindow), _
                             New FrameworkPropertyMetadata(Nothing))
    
        Public Property WindowWidth As Integer
            Get
                Return CInt(GetValue(WindowWidthProperty))
            End Get
            Set(ByVal value As Integer)
                SetValue(WindowWidthProperty, value)
            End Set
        End Property
    
        Public Property WindowHeight As Integer
            Get
                Return CInt(GetValue(WindowHeightProperty))
            End Get
            Set(ByVal value As Integer)
                SetValue(WindowHeightProperty, value)
            End Set
        End Property
    
    
    End Class
    

    C# Code

    public readonly DependencyProperty WindowWidthProperty = DependencyProperty.Register("WindowWidth", typeof(Double), typeof(MainWindow), new FrameworkPropertyMetadata());
    public readonly DependencyProperty WindowHeightProperty = DependencyProperty.Register("WindowHeight", typeof(Double), typeof(MainWindow), new FrameworkPropertyMetadata());
    
    public double WindowWidth {
        get { return Convert.ToDouble(this.GetValue(WindowWidthProperty)); }
        set { this.SetValue(WindowWidthProperty, value); }
    }
    
    public double WindowHeight {
        get { return Convert.ToDouble(this.GetValue(WindowHeightProperty)); }
        set { this.SetValue(WindowHeightProperty, value); }
    }
    

提交回复
热议问题