@OwDafuq

WPF, как сделать биндинг в своих контролах?

Есть свой контрол:
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);
    }
}


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

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


Пытаюсь сделать биндинг к св-ву Source и сделать блюр:
<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>


Биндинг к Source работает, а вот для Блюра вылетает ошибка:
System.Windows.Data Error: 40 : BindingExpression path error: 'Settigns' property not found on 'object' ''BlurEffect' (HashCode=43556055)'. BindingExpression:Path=Settigns.Blur; DataItem='BlurEffect' (HashCode=43556055); target element is 'BlurEffect' (HashCode=43556055); target property is 'Radius' (type 'Double')


Как решить проблему?

p.s. - а если кто-то предложит более удобный вариант биндинга для своих свойств в контролах - буду премного благодарен
  • Вопрос задан
  • 81 просмотр
Пригласить эксперта
Ответы на вопрос 1
@OwDafuq Автор вопроса
Решение:
<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>
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы