如何将WPF TextBox绑定到作为应用程序主窗口成员的属性

use*_*773 2 .net c# wpf xaml binding

我有一个私人领域

private static Double myValue;
Run Code Online (Sandbox Code Playgroud)

在应用程序MainWindow类中.并且(在MainWindow类中)我定义了一个属性

public static Double MytValue
{
    get { return myValue; }
}
Run Code Online (Sandbox Code Playgroud)

在MainWindow类的结构中,我有一个TextBox.我需要将它绑定到MytValue属性.在XAML中我写道:

<TextBox Name="tbxMyValue" Grid.Row="1" Grid.Column="2" TextAlignment="Center"
         Text="{Binding Path=MyValue}" Width="Auto" Margin="10,0,10,15" IsEnabled="True" />
Run Code Online (Sandbox Code Playgroud)

但它没有效果.当myValue变量有值时,我在TextBox中看不到任何内容.为什么?请帮我.

pap*_*zzo 5

我喜欢在Window部分设置DataContext

<Window x:Class="Gabe3a.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Gabe3a"
        xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        DataContext="{Binding RelativeSource={RelativeSource self}}"
        Title="Gabriel Main Ver 3a01" Icon="faviconw.ico" Height="600" Width="800">
Run Code Online (Sandbox Code Playgroud)