0

I'm trying to achieve something that should be pretty simple but I'm stuck.

Here what I'm trying to do: a UserControl that is just a FontAwesome icon in front of a border.

Here the xaml of my UC

<UserControl
x:Class="Project.Views.UC.UC_CircledIcons"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Project.Views.UC"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:fa="using:FontAwesome5.UWP"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
d:DesignHeight="200"
d:DesignWidth="200">
<Border Style="{StaticResource BorderStyle1}"  Height="{Binding Height}" Width="{Binding Width}" BorderBrush="{Binding MyColor}" VerticalAlignment="Center" HorizontalAlignment="Center">
    <fa:FontAwesome Foreground="{Binding MyColor}" Icon="{Binding MyIcon}" Height="{Binding Height}" Width="{Binding Width}" FontSize="{Binding MyFontSize}" VerticalAlignment="Center" HorizontalAlignment="Center"></fa:FontAwesome>
</Border>

Here the cs code of my UC :

namespace Project.Views.UC
{
    public sealed partial class UC_CircledIcons : UserControl
    {
        public UC_CircledIcons()
        {
            this.InitializeComponent();
            this.Height = 200;
            this.Width = 200;
        }

        /// <summary>
        /// Le font size de l'icon est égal à 2.6 fois sa hauteur
        /// </summary>
        public double MyFontSize
        {
            get
            {
                return (double)GetValue(HeightProperty) / 2.6;
            }
        }

        /// <summary>
        /// Pour setter l'icone FontAwesome du composant via l'enum
        /// </summary>
        public static readonly DependencyProperty IconProperty = DependencyProperty.Register("MyIcon", typeof(EFontAwesomeIcon), typeof(UC_CircledIcons), new PropertyMetadata(default(EFontAwesomeIcon)));
        public EFontAwesomeIcon MyIcon
        {
            get
            {
                return (EFontAwesomeIcon)GetValue(IconProperty);
            }
            set
            {
                SetValue(IconProperty, value);
            }
        }

        /// <summary>
        /// Pour setter la color du border et de l'icone en même temps
        /// </summary>
        public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("MyColor", typeof(string), typeof(UC_CircledIcons), new PropertyMetadata(default(string)));
        public string MyColor
        {
            get {
                return (string)GetValue(ColorProperty);
            }
            set {
                SetValue(ColorProperty, value);
            }
        }
    }
}

This is working fine if I use my UserControls statically in my xaml page like this:

<uc:UC_CircledIcons MyColor="#321654" MyIcon="Solid_Check"></uc:UC_CircledIcons>

But I'm trying to dynamically set the color and the icon of this UC. So I want to use binding to achieve this. Something like :

<uc:UC_CircledIcons MyColor="{Binding MainIconColor}" MyIcon="{Binding MainIcon}"></uc:UC_CircledIcons>

Like I'm doing with any Textblock content by binding to any property of my ViewModel. But with my UserControl this is not working. In the output windows i can see the binding error :

Error: BindingExpression path error: 'MainIcon' property not found on 'Project.Views.UC.UC_CircledIcons'. BindingExpression: Path='MainIcon' DataItem='Project.Views.UC.UC_CircledIcons'; target element is 'Project.Views.UC.UC_CircledIcons' (Name='null'); target property is 'MyIcon' (type 'EFontAwesomeIcon')

And I believe this is caused by the line :

DataContext="{Binding RelativeSource={RelativeSource Self}}"

In my UserControl's code. It seems like xaml is looking for a property named MainIcon in my USerControl's definition or this property is part of my ViewModel.

Where am I missing something ? Is there any way to achieve what I'm trying to do ? Thanks a lot for help.

1 Answer 1

1

Replace DataContext="{Binding RelativeSource={RelativeSource Self}}" with Name="uc" and bind to the properties using an ElementName:

<Border ... BorderBrush="{Binding MyColor, ElementName=uc}" ... />

Then the UserControl should inherit its DataContext as expected, but you will still be able to bind to properties of the control itself in its XAML markup.

Sign up to request clarification or add additional context in comments.

1 Comment

It did the job. Thanks a lot !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.