插入不添加到属性
中的变量private string _DirectoryList = "";
public string DirectoryListAdd
{
get { return _DirectoryList; }
set
{
_DirectoryList.Insert(0, value + Environment.NewLine);
OnPropertyChanged("DirectoryListAdd");
}
}
public MainWindow()
{
InitializeComponent();
DirectoryListAdd = "a";
DirectoryListAdd = "b";
Console.WriteLine(DirectoryListAdd);
}
这回归""。我希望它能回归
a
b
答案 0 :(得分:3)
https://msdn.microsoft.com/en-us/library/system.string.insert(v=vs.110).aspx
返回一个新字符串,在该字符串中,在此实例中的指定索引位置插入指定的字符串。
_DirectoryList = _DirectoryList.Insert(0, value + Environment.NewLine);
+1代表“不要那样使用属性”
答案 1 :(得分:0)
private string _DirectoryList = "";
public string DirectoryListAdd
{
get { return _DirectoryList; }
set
{
_DirectoryList=_DirectoryList.Insert(0, value + Environment.NewLine);
OnPropertyChanged("DirectoryListAdd");
}
}
public MainWindow()
{
InitializeComponent();
DirectoryListAdd = "a";
DirectoryListAdd = "b";
Console.WriteLine(DirectoryListAdd);
}
请注意_DirectoryList=_DirectoryList.Insert(0, value + Environment.NewLine);
此方法会在字符串的begninng中插入,而不是像你想要的那样。所以记住这一点。替换0 _DirectoryList.Length-1
也像其他评论所说的那样,不要以这种方式使用属性。它比它的价值更令人困惑