WPF文本框文本绑定

6
我试图将文本框的文字绑定到我的类中的属性上,但它没有起作用。我在代码后台编辑属性,但我没有在文本框中看到字符串。 这是该类,我试图绑定的属性名为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;
    }

}

这里有一个 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()

在调试过程中,我发现该属性已经发生了变化,但文本框中的文本没有改变。


你的通知事件中有错误的属性:NotifyPropertyChanged("sPath"); 应该是 NotifyPropertyChanged("songsFolder") - McGarnagle
谢谢,我已经修改了,但它仍然无法工作。 - alostr
1
如果您能解释一下具体哪里出了问题,而不仅仅是说“不起作用”,那会对我们有所帮助... - McGarnagle
我没有改变XAML。 - alostr
你是否遇到了任何绑定错误(请查看输出窗口)?如果是,请发布详细信息。如果您使用MVVM,请向我们展示mainWindow类的代码后台(或viewModel)。 - Benjamin Gale
1个回答

2
传递到NotifyPropertyChanged事件中的字符串应该与属性本身的名称相同。
public string songsFolder 
{ 
    get 
    { 
      return folder; 
    } 
    set 
    { 
      folder = value; 
      NotifyPropertyChanged("songsFolder"); 
    }
}

此外,请尝试将UpdateSourceTrigger =“ PropertyChanged”添加到textBox的绑定中。
<TextBox Name="browseBox" Text="{Binding Source={StaticResource ResourceKey=song}, Path=songsFolder, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="0"></TextBox>

编辑:也许DataContext没有被正确地设置。您还可以尝试这种方法(不使用静态Key)。

窗口的构造函数中的代码:

browseBox.DataContext = new song();

然后,将textBox查找更新为:
<TextBox Name="browseBox" Text="{Binding Path=songsFolder, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="0"></TextBox>

已添加,但仍然无法正常工作。在代码后台,我打开了一个文件夹浏览器对话框,并根据所选路径更改了songsFolder属性,但是我在文本框中看不到它。 - alostr
@user1622986 已更新。设置窗口的数据上下文(请参见编辑)。 - d.moncada
无法这样做,我正在使用窗口数据上下文将歌曲列表绑定到列表框。 - alostr
1
@user1622986 将 public static string folder; 改为 private string folder; - d.moncada

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接