wpf 文本框文本绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13733849/
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
wpf textbox text binding
提问by alostr
i am trying to bind the text of a textbox to a property in my class, and it is not working, I am editing the property in the code behind but I don't see the string in the textbox this is the class, and the property i am trying to bind is called songFolder.
我正在尝试将文本框的文本绑定到我班级中的一个属性,但它不起作用,我正在后面的代码中编辑该属性,但我没有在文本框中看到字符串,这是该类,并且我试图绑定的属性称为 songFolder。
public class song : INotifyPropertyChanged
{
public string title {get; set; }
public string artist { get; set; }
public string path { get; set; }
public static string folder;
public string songsFolder { get { return folder; } set { folder = value; NotifyPropertyChanged("songsFolder"); } }
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public song()
{
}
public song(string title, string artist, string path)
{
this.title = title;
this.artist = artist;
this.path = path;
}
}
and the xaml, containing the resource and the textbox wich i am tring to bind
和 xaml,包含我想要绑定的资源和文本框
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Song Filler" Height="455" Width="525">
<Window.Resources>
<local:song x:Key="song"/>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<TextBox Name="browseBox" Text="{Binding Source={StaticResource ResourceKey=song}, Path=songsFolder, Mode=TwoWay}" Grid.Column="0"></TextBox>
<Button Grid.Column="1" Width="auto" Click="Browse">browse</Button>
</Grid>
--------------update---------------- I added the next line to ctor of the window:
--------------更新---------------- 我在窗口的ctor中添加了下一行:
BrowseBox.DataContext=new song()
And while debugging I saw that the property is changing but the text in the textbox isn't.
在调试时,我看到属性正在改变,但文本框中的文本没有。
回答by d.moncada
The string passed into the NotifyPropertyChanged event should be the same name of the property itself.
传递给 NotifyPropertyChanged 事件的字符串应该与属性本身的名称相同。
public string songsFolder
{
get
{
return folder;
}
set
{
folder = value;
NotifyPropertyChanged("songsFolder");
}
}
Also,
还,
try adding UpdateSourceTrigger="PropertyChanged" to the binding of the textBox
尝试将 UpdateSourceTrigger="PropertyChanged" 添加到 textBox 的绑定中
<TextBox Name="browseBox" Text="{Binding Source={StaticResource ResourceKey=song}, Path=songsFolder, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="0"></TextBox>
Edit: Maybe the DataContext is not getting set correctly. You can also try this method (W/out a static Key)
编辑:也许 DataContext 没有正确设置。你也可以试试这个方法(W/out a static Key)
Code behind, inside the Ctor of the window:
后面的代码,在窗口的ctor里面:
browseBox.DataContext = new song();
Then, update textBox finding to:
然后,将 textBox 发现更新为:
<TextBox Name="browseBox" Text="{Binding Path=songsFolder, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="0"></TextBox>

