-
Notifications
You must be signed in to change notification settings - Fork 0
BindingProxyMarkup en
Leksiqq edited this page May 8, 2023
·
4 revisions
Attention! This article, as well as this announcement, are automatically translated from Russian.
Used when a binding value needs to be placed where a markup extension is required.
BindingProxy
is an object of type BindingProxy
containing the binding.
Suppose we need to use a specific instance of the converter or different converters, depending on some conditions.
In AboutConverter.xaml.cs
:
...
public IUniversalConverter Converter { get; init; }
public AboutConverter(IUniversalConverter converter)
{
Converter = Converter;
InitializeComponent();
}
...
In AboutConverter.xaml
:
<Window.Resources>
<ResourceDictionary>
<l:BindingProxy Value="{Binding Converter}" x:Key="ConverterProxy"/>
<l:BindingProxyMarkup x:Key="Converter" BindingProxy="{StaticResource ConverterProxy}"/>
</ResourceDictionary>
</Window.Resources>
<DockPanel LastChildFill="True">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock Text="Using " FontSize="24"
Foreground="{Binding Converter={StaticResource Converter}, ConverterParameter=Color}"/>
<TextBlock Text="{Binding Converter, Converter={StaticResource Converter}, ConverterParameter=Type}"
FontSize="24"
Foreground="{Binding Converter={StaticResource Converter}, ConverterParameter=Color}"/>
</StackPanel>
</DockPanel>
In MainWindow.xaml
, pressing the corresponding button selects a converter for the dialog that opens:
<Button x:Name="Red" Content="Red Converter" Click="AboutConverter_Click" Margin="10,10,10,10"/>
<Button x:Name="Blue" Content="Blue Converter" Click="AboutConverter_Click" Margin="10,10,10,10"/>
In MainWindow.xaml.cs
:
private void AboutConverter_Click(object sender, RoutedEventArgs e)
{
if(sender is Button button)
{
switch(button.Name)
{
case "Red":
new AboutConverter(new RedConverter()).ShowDialog();
break;
case "Blue":
new AboutConverter(new BlueConverter()).ShowDialog();
break;
}
}
}
This example is taken from Demo.
Before: (BindingProxy) Start: (Overview) Next: (IUniversalConverter)