BindingHelper.cs 788 B

123456789101112131415161718192021222324252627282930
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Data;
  4. namespace HandyControl.Tools.Helper;
  5. public class BindingHelper
  6. {
  7. public static string GetString(object source, string path)
  8. {
  9. if (string.IsNullOrEmpty(path))
  10. {
  11. return source == null ? string.Empty : source.ToString();
  12. }
  13. var tempObj = new DependencyObject();
  14. var binding = new Binding(path)
  15. {
  16. Mode = BindingMode.OneTime,
  17. Source = source
  18. };
  19. BindingOperations.SetBinding(tempObj, TextSearch.TextProperty, binding);
  20. var result = (string) tempObj.GetValue(TextSearch.TextProperty);
  21. BindingOperations.ClearBinding(tempObj, TextSearch.TextProperty);
  22. return result;
  23. }
  24. }