L
L
LiptonOlolo2020-06-24 04:36:46
WPF
LiptonOlolo, 2020-06-24 04:36:46

WPF, how to bind in your controls?

There is a control:

public partial class BackgroundImage : Image
{
    public static DependencyProperty SettingsProperty = DependencyProperty.Register("Settings", typeof(BackgroundSettings), typeof(BackgroundImage));

    public BackgroundImage()
    {
        InitializeComponent();
    }

    public BackgroundSettings Settings
    {
        get => (BackgroundSettings)GetValue(SettingsProperty);
        set => SetValue(SettingsProperty, value);
    }
}


Where BackgroundSettings:
public class BackgroundSettings : ViewModelBase
{
    /// <summary>
    /// Image path.
    /// </summary>
    public string ImagePath { get; set; }

    /// <summary>
    /// Image blur.
    /// </summary>
    public int Blur { get; set; }
}


I'm trying to bind to the Source sv and make a blur:
<Image
    x:Class="Malinka.Controls.BackgroundImage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Source="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Settings.ImagePath, Converter={StaticResource BackgroundImageConverter}}"
    Stretch="UniformToFill">

    <Image.Effect>
        <BlurEffect Radius="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Settigns.Blur}"/>
    </Image.Effect>
</Image>


Binding to Source works, but for Blur an error occurs:
System.Windows.Data Error: 40 : BindingExpression path error: 'Settigns' property not found on 'object' ''BlurEffect' (HashCode=43556055)'. BindingExpression:Path=Settings.Blur; DataItem='BlurEffect' (HashCode=43556055); target element is 'BlurEffect' (HashCode=43556055); target property is 'Radius' (type 'Double')


How to solve the problem?

ps - and if someone offers a more convenient binding option for their properties in controls, I will be very grateful

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LiptonOlolo, 2020-06-24
@LiptonOlolo

Decision:

<Image
    x:Class="Malinka.Controls.BackgroundImage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Name="imageBase"
    mc:Ignorable="d"
    Source="{Binding Settings.ImagePath, ElementName=imageBase, Converter={StaticResource BackgroundImageConverter}}"
    Stretch="UniformToFill">

    <Image.Effect>
        <BlurEffect Radius="{Binding Settings.Blur, ElementName=imageBase}"/>
    </Image.Effect>

</Image>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question