【问题标题】:wpf textbox text bindingwpf 文本框文本绑定
【发布时间】:2012-11-23 21:43:58
【问题描述】:

我正在尝试将文本框的文本绑定到我的类中的属性,但它不起作用,我正在后面的代码中编辑属性,但我没有在文本框中看到字符串 这是类,我要绑定的属性称为 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>

-------------更新---- 我在窗口的ctor中添加了下一行:

BrowseBox.DataContext=new song()

在调试时,我看到属性正在更改,但文本框中的文本没有。

【问题讨论】:

  • 您的通知事件中有错误的属性:NotifyPropertyChanged("sPath"); 应该是NotifyPropertyChanged("songsFolder")
  • 谢谢,我改了,还是不行
  • 如果您解释除了“不工作”之外的问题,这可能会对我们有所帮助......
  • 我没有更改 xaml
  • 您是否遇到任何绑定错误(查看输出窗口)?如果是这样,请发布详细信息。如果您向我们展示 mainWindow 类(或您的 viewModel,如果您使用 MVVM)的代码隐藏,它也可能会有所帮助。

标签: wpf binding textbox inotifypropertychanged


【解决方案1】:

传递给 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 设置不正确。你也可以试试这个方法(不用静态键)

代码后面,在窗口的 Ctor 内:

browseBox.DataContext = new song();

然后,将文本框结果更新为:

<TextBox Name="browseBox" Text="{Binding Path=songsFolder, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="0"></TextBox>

【讨论】:

  • 添加了,还是不行。在后面的代码中,我打开了一个文件夹浏览器对话框并根据所选路径更改了 songFolder 属性,但我在文本框中没有看到它
  • @user1622986 更新..设置窗口的dataContext(见编辑)
  • 不能这样,我正在使用窗口dataContext将歌曲列表绑定到列表框。
  • @user1622986 更改公共静态字符串文件夹;到私有字符串文件夹;
猜你喜欢
  • 2012-11-24
  • 1970-01-01
  • 2011-03-29
  • 2015-10-16
  • 2010-12-20
  • 2018-01-24
  • 2016-07-22
  • 1970-01-01
  • 2010-11-13
相关资源
最近更新 更多