C#将类字段绑定到Textbox

时间:2016-03-08 19:59:07

标签: c# wpf xaml data-binding

我想将属性FilePath从类SExcelFile绑定到textblox。这是我的文件结构。

enter image description here

我的MainWindow.xaml内部只是一个textblox。

<TextBox Text="{Binding FilePath}" Grid.ColumnSpan="2" Grid.Row="3" Margin="5" Padding="5"/>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public SManagerRoot ManagerRoot = new SManagerRoot();

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this.ManagerRoot.ExcelFileManager.ExcelFile;
    }
}

SManagerRoot.cs

public class SManagerRoot
{
    #region Properties

    public SExcelFileManager ExcelFileManager = new SExcelFileManager();

    #endregion Properties
}

SExcelFileManager.cs

public class SExcelFileManager
{
    #region Properties

    public SExcelFile ExcelFile { get; set; }
    public SPersonManager PersonManager = new SPersonManager();

    #endregion Properties
}

SExcelFile.cs

public class SExcelFile : INotifyPropertyChanged
{
    #region Properties

    private string _filePath;
    public string FilePath
    {
        set { this._filePath = value; OnPropertyChanged("FilePath"); }
        get { return this._filePath; }
    }

    #endregion Properties

    #region c'tor

    public SExcelFile()
    {

    }

    public SExcelFile(string filePath)
    {
        this.FilePath = filePath;
    }

    #endregion c'tor

    #region PropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(name));
    }

    #endregion PropertyChanged
}

我添加了一个datacontext。 Binding是属性FilePath,当值更改时调用onpropertychanged。问题在哪里?

0 个答案:

没有答案