当在 VM 构造函数中给定属性值时,设计器中不显示文本框的 WPF 数据绑定
WPF data binding for textbox not showing in designer when properties are given values in the VM constructor
我正在设置绑定的默认值。我在 ViewModel class 构造函数中设置此类属性的值。
这里是ViewModel文件的代码片段-
private Contacts contacts;
public Contacts Contacts
{
get { return contacts; }
set
{
contacts = value;
OnPropertyChanged("Contacts");
}
}
构造函数-
public MainWindowVM()
{
Contacts = new Contacts
{
Contact = "Import/Paste Contacts here"
};
Import = new Import(this);
Send = new Send(this);
}
这是 Xaml 文件片段-
<Window x:Class="WhatsAppBulkSender_WPF.View.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:WhatsAppBulkSender_WPF.View"
xmlns:vm ="clr-namespace:WhatsAppBulkSender_WPF.ViewModel"
mc:Ignorable="d"
Background="#E9E9E9"
Title="MainWindow" Height="450" Width="800" MinHeight="450" MinWidth="800">
<Window.Resources>
<vm:MainWindowVM x:Key="vm"/>
</Window.Resources>
----
<TextBox x:Name="ContactsTextBox"
DataContext="{Binding Contacts}"
Text="{Binding Contact, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource txtBoxStyle}"
Grid.Column="3"
Grid.ColumnSpan="2"
Grid.RowSpan="5"/>
问题 - 绑定工作正常,我也能看到构造函数后的变化。但是我在构造函数中设置的值没有显示在设计器中或 运行 时间。
截图
(图像显示上述值未在设计器中显示/xaml)
在模型 class 中,我有成员。我试图使用 getter 和 setter 从 class 外部更改 odf thos 成员的值,但首先我没有将这些变量声明为 属性.
因此,在将它们声明为属性后,事情就开始起作用了。
有时候一个小错误会给你带来很大的麻烦。
我正在设置绑定的默认值。我在 ViewModel class 构造函数中设置此类属性的值。
这里是ViewModel文件的代码片段-
private Contacts contacts;
public Contacts Contacts
{
get { return contacts; }
set
{
contacts = value;
OnPropertyChanged("Contacts");
}
}
构造函数-
public MainWindowVM()
{
Contacts = new Contacts
{
Contact = "Import/Paste Contacts here"
};
Import = new Import(this);
Send = new Send(this);
}
这是 Xaml 文件片段-
<Window x:Class="WhatsAppBulkSender_WPF.View.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:WhatsAppBulkSender_WPF.View"
xmlns:vm ="clr-namespace:WhatsAppBulkSender_WPF.ViewModel"
mc:Ignorable="d"
Background="#E9E9E9"
Title="MainWindow" Height="450" Width="800" MinHeight="450" MinWidth="800">
<Window.Resources>
<vm:MainWindowVM x:Key="vm"/>
</Window.Resources>
----
<TextBox x:Name="ContactsTextBox"
DataContext="{Binding Contacts}"
Text="{Binding Contact, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource txtBoxStyle}"
Grid.Column="3"
Grid.ColumnSpan="2"
Grid.RowSpan="5"/>
问题 - 绑定工作正常,我也能看到构造函数后的变化。但是我在构造函数中设置的值没有显示在设计器中或 运行 时间。
截图
(图像显示上述值未在设计器中显示/xaml)
在模型 class 中,我有成员。我试图使用 getter 和 setter 从 class 外部更改 odf thos 成员的值,但首先我没有将这些变量声明为 属性.
因此,在将它们声明为属性后,事情就开始起作用了。
有时候一个小错误会给你带来很大的麻烦。