Advertisement
Guest User

Untitled

a guest
Sep 20th, 2023
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. public class MyConverter : Freezable, IValueConverter
  2. {
  3. // Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc...
  4. public static readonly DependencyProperty MainContextProperty =
  5. DependencyProperty.Register(nameof(MainContext), typeof(DependencyObject), typeof(MyConverter),
  6. new UIPropertyMetadata(null));
  7.  
  8. public DependencyObject MainContext
  9. {
  10. get => (DependencyObject)GetValue(MainContextProperty);
  11. set => SetValue(MainContextProperty, value);
  12. }
  13.  
  14. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  15. {
  16. var window = Window.GetWindow(MainContext);
  17.  
  18. throw new NotImplementedException();
  19. }
  20.  
  21. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) =>
  22. throw new NotImplementedException();
  23.  
  24. #region Overrides of Freezable
  25.  
  26. protected override Freezable CreateInstanceCore() => new MyConverter();
  27.  
  28. #endregion
  29. }
  30.  
  31. <UserControl
  32. x:Class="WpfApp1.UserControl1"
  33. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  34. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  35. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  36. xmlns:local="clr-namespace:WpfApp1"
  37. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  38. d:DesignHeight="450"
  39. d:DesignWidth="800"
  40. Name="Root"
  41. mc:Ignorable="d">
  42. <UserControl.Resources>
  43. <local:MyConverter x:Key="MyConverter" MainContext="{Binding ElementName=Root}" />
  44. </UserControl.Resources>
  45. <Grid Background="{Binding Foo, Mode=OneTime, Converter={StaticResource MyConverter}}" />
  46. </UserControl>
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement