如何在WPF中检索控件的属性,如x,y,高度,宽度,文本等?

时间:2013-11-28 09:29:33

标签: c# wpf wpf-controls

我是c#

知识有限的初学者

enter image description here

如何在创建表单时检索其坐标并将其保存在文本文件中?

1 个答案:

答案 0 :(得分:0)

        private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        // Persist application-scope property to isolated storage
        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain();
        using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("abc.txt", FileMode.Create, storage))
        using (StreamWriter writer = new StreamWriter(stream))
        {
            foreach (var keyValuePair in GetProperties(this))
            {
                writer.WriteLine("{0},{1}", keyValuePair.Key, keyValuePair.Value);
            }
        }
    }

    private Dictionary<string,string> GetProperties(Window window)
    { 
        return new Dictionary<string,string>{
        {"Left",window.Left.ToString()},
        {"Top",window.Top.ToString()},
        {"Width",window.Width.ToString()},
        {"Height",window.Height.ToString()}
    };
    }