Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyConverter : Freezable, IValueConverter
- {
- // Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty MainContextProperty =
- DependencyProperty.Register(nameof(MainContext), typeof(DependencyObject), typeof(MyConverter),
- new UIPropertyMetadata(null));
- public DependencyObject MainContext
- {
- get => (DependencyObject)GetValue(MainContextProperty);
- set => SetValue(MainContextProperty, value);
- }
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- var window = Window.GetWindow(MainContext);
- throw new NotImplementedException();
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) =>
- throw new NotImplementedException();
- #region Overrides of Freezable
- protected override Freezable CreateInstanceCore() => new MyConverter();
- #endregion
- }
- <UserControl
- x:Class="WpfApp1.UserControl1"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:local="clr-namespace:WpfApp1"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- d:DesignHeight="450"
- d:DesignWidth="800"
- Name="Root"
- mc:Ignorable="d">
- <UserControl.Resources>
- <local:MyConverter x:Key="MyConverter" MainContext="{Binding ElementName=Root}" />
- </UserControl.Resources>
- <Grid Background="{Binding Foo, Mode=OneTime, Converter={StaticResource MyConverter}}" />
- </UserControl>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement