我想將文本框的文本綁定到我的類中的某個屬性,但它不工作,我在後面的代碼中編輯屬性,但在文本框中看不到該字符串 這是類,我試圖綁定的屬性叫做songFolder。wpf文本框文本綁定
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;
}
}
和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>
--------------最新情況: --------------- 我加入下一行到窗口的構造函數:
BrowseBox.DataContext=new song()
雖然調試只見該屬性爲c掛但文本框中的文本不是。
您的通知事件中有一個錯誤的屬性:'NotifyPropertyChanged(「sPath」);'應該是'NotifyPropertyChanged(「songsFolder」)'。 – McGarnagle
謝謝,我改變了它,但它仍然不起作用 – alostr
如果你解釋除了「不工作」之外的其他問題,它可能會幫助我們... – McGarnagle