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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12845376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:44:24  来源:igfitidea点击:

How to bind WPF TextBox to a propperty that is the member of application main window

c#.netwpfxamlbinding

提问by user1688773

I have a private field

我有一个私人领域

private static Double myValue;

in the application MainWindow class. And there (in the MainWindow class) I defined a property

在应用程序 MainWindow 类中。在那里(在 MainWindow 类中)我定义了一个属性

public static Double MytValue
{
    get { return myValue; }
}

In the structure of the MainWindow class I have a TextBox. I'm in need of binding it to the MytValue property. In XAML I write:

在 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" />

But it has no effect. I see nothing in the TextBox while myValue variable has a value. Why? Please help me.

但它没有效果。我在 TextBox 中什么也看不到,而 myValue 变量有一个值。为什么?请帮我。

回答by paparazzo

I like to set the DataContext in the Window section

我喜欢在 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">

回答by Rachel

You need to set the DataContextbehind the Window for that binding to work

您需要DataContext在 Window 后面设置该绑定才能工作

There are two layers to an application with WPF: the UI layer and the data layer.

使用 WPF 的应用程序有两层:UI 层和数据层。

The Data layer for an application starts out as null, and you can set it using the DataContextproperty.

应用程序的数据层以 开头null,您可以使用DataContext属性设置它。

Whenever you do a basic binding in WPF, you are binding to the DataContext. So Text="{Binding Path=MyValue}"is actually saying "Get the MyValueproperty from the current DataContext".

每当您在 WPF 中进行基本绑定时,您都将绑定到DataContext. 所以Text="{Binding Path=MyValue}"实际上是在说“MyValue从当前获取财产DataContext”。

You could simply set the DataContextin your code behind:

你可以简单地DataContext在你的代码后面设置:

MyWindow.DataContext = this;

Or you can use a RelativeSourcebinding to tell WPF to get the MyValueproperty from somewhere else, such as telling it to get it from the closest Window it finds when navigating up the VisualTree:

或者您可以使用RelativeSource绑定来告诉 WPFMyValue从其他地方获取属性,例如告诉它从它在导航时找到的最近的窗口获取它VisualTree

Text="{Binding Path=MyValue, RelativeSource={
    RelativeSource AncestorType={x:Type Window}}}"

I actually have an article on my blog about the DataContextthat I'd recommend reading if you're new to WPF and the DataContext: What is this "DataContext" you speak of?

实际上,我的博客上有一篇关于DataContext如果您不熟悉 WPF 和我建议您阅读的文章DataContext您所说的“DataContext”是什么?

回答by Erti-Chris Eelmaa

It's not how it works. Binding to static properties; {Binding Source={x:Static local:Application.MyValue}}

这不是它的工作原理。绑定到静态属性;{绑定源={x:静态本地:Application.MyValue}}

Note that your field needs to be property & public. If you want to go with your solution, you need to set DataContext as {RelativeSource Self}.

请注意,您的领域必须是财产和公共领域。如果您想使用您的解决方案,您需要将 DataContext 设置为 {RelativeSource Self}。

回答by kmatyaszek

In file MainWindow.xaml.cs in constructor add this line:

在构造函数的文件 MainWindow.xaml.cs 中添加以下行:

DataContext = this;

after InitializeComponent();

InitializeComponent();