C# - WPF-绑定问题 - 无法绑定XAML中的C#属性
我刚刚开始使用C#中的WPF,目前使用Visual Studio 2019,.NET Framework 4.7.2中的WPF应用程序。
几天来,我一直在努力绑定数据,浏览了许多教程和网站,试图弄清楚我缺少的东西。此时,我将当前的项目搁置了,并启动了新的WPF .NET Framework应用程序(无MVVM或任何其他附加软件包,只有C#和WPF XAML),以查看我是否可以解决这个问题,而且我没有运气。
对于我的寿命,我无法在mainwindow.xaml.cs文件中获得单个公共变量,该变量可在XAML文件中绑定。例如,如果我在CS文件中添加了一个公共字符串属性(testString),则使用一个Geter和setter,当我尝试将文本框设置为绑定到它时,它不会显示出来,无论我使用绑定的测试标准还是绑定路径= testString,它总是显示“找不到用于绑定'testString'的DataContext,并且对于我尝试过的每种其他类型和控件都是这样的。
我尝试添加“ This.dataContext = this;” MainWindow CS File的构造函数,这没有什么区别,当程序运行时,我在其他教程视频中注意到的东西都不会发生。从来没有看到,在文本框或任何其他类型的控件中添加bing时,我会得到的唯一的智能感是诸如Asyncstate,BindingGroupName,BindsDirectlytosource,Converter,Converterculture等等的列表。选项,代码范围文件中的任何可用公共属性都没有IntelliSense。
以下是我在XAML中所拥有的:
<Window x:Class="WPFTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Path=TestString}"></TextBox>
</Grid>
</Window>
这就是我在mainwindow.xaml.cs文件中所拥有的:
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
namespace WPFTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : INotifyPropertyChanged
{
private string _testString;
public string TestString
{
get
{
return _testString;
}
set
{
_testString = value;
OnPropertyChanged();
MessageBox.Show(_testString);
}
}
public MainWindow()
{
DataContext = this;
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}
我在测试串的设置中添加了一个消息框,只是为了查看它是否已设置,而不是。该程序编译并运行,但不会发生绑定。在我的主要项目(具有GridView)中,我可以从C#集合中显示数据的唯一方法是我将其从文件背后的代码强加到GridView中。
您可以提供的任何帮助将不胜感激。
I'm just getting started with WPF in C#, currently using Visual Studio 2019, WPF application in .NET Framework 4.7.2.
I've been struggling with data binding for a few days now, going through numerous tutorials and websites, trying to figure out what I'm missing. At this point, I have placed my current project on hold and started a fresh, new WPF .NET Framework application (no MVVM or any other add-in packages, just C# and WPF XAML) to see if I can get this sorted out, and I am not having any luck.
For the life of me, I cannot get a single public variable that is declared in the MainWindow.xaml.cs file to be bindable in the XAML file. If, for example, I add a public string property (TestString) to the CS file, with a getter and setter, it does not show up when I try to set a textbox Binding to it, whether I use Binding TestString, or Binding Path=TestString, it always shows "No DataContext found for Binding 'TestString', and it has been this way for every other type and control I have tried.
I have tried adding "this.DataContext = this;" to the MainWindow CS file's constructor, and that makes no difference, the binding does not occur when the program is running either. Something I had noticed in other tutorial videos is that when people add the binding statements to the controls, they have intellisense showing the available options, but I have never seen that, the only intellisense I get when adding a Binging to a textbox, or any other type of control, is a list of things like AsyncState, BindingGroupName, BindsDirectlyToSource, Converter, ConverterCulture, and so on. If I select Path from those options, there is no intellisense for any available public properties in the code-behind file.
Below is what I have in the XAML:
<Window x:Class="WPFTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Path=TestString}"></TextBox>
</Grid>
</Window>
And this is what I have in the MainWindow.xaml.cs file:
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
namespace WPFTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : INotifyPropertyChanged
{
private string _testString;
public string TestString
{
get
{
return _testString;
}
set
{
_testString = value;
OnPropertyChanged();
MessageBox.Show(_testString);
}
}
public MainWindow()
{
DataContext = this;
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}
I added a message box to the setter of the TestString just to see if it was ever getting set, and it is not. The program compiles and runs, but the binding does not occur. In my main project, which has a GridView, the only way I can get it to show the data from the C# collection is if I force it into the GridView from the code behind file.
Any help you can provide would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:我还在Reddit R/Csharp上发布了这个问题,并收到了一些非常不错的答复。这个问题有两个部分答案:
校正项目的示例:
Update: I had also posted this question on Reddit r/csharp and I received some really good replies. There is a two part answer to this question:
Example of corrected items: