【问题标题】:Binding issue in UserControlUserControl 中的绑定问题
【发布时间】:2019-03-24 02:08:46
【问题描述】:

我正在尝试实现一些应该很简单的事情,但我被困住了。

这就是我想要做的:一个 UserControl,它只是一个位于边框前面的 FontAwesome 图标。

这里是我的 UC 的 xaml

<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>

这里是我的 UC 的 cs 代码:

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);
            }
        }
    }
}

如果我像这样在我的 xaml 页面中静态使用我的 UserControls,这会正常工作:

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

但我正在尝试动态设置此 UC 的颜色和图标。所以我想使用绑定来实现这一点。类似的东西:

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

就像我通过绑定到我的 ViewModel 的任何属性来处理任何 Textblock 内容一样。但是对于我的 UserControl,这是行不通的。在输出窗口中我可以看到绑定错误:

错误:BindingExpression 路径错误:在“Project.Views.UC.UC_CircledIcons”上找不到“MainIcon”属性。 BindingExpression: Path='MainIcon' DataItem='Project.Views.UC.UC_CircledIcons';目标元素是'Project.Views.UC.UC_CircledIcons'(名称='null');目标属性是“MyIcon”(类型“EFontAwesomeIcon”)

我相信这是由这条线引起的:

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

在我的 UserControl 的代码中。似乎 xaml 正在我的 USerControl 的定义中寻找名为 MainIcon 的属性,或者此属性是我的 ViewModel 的一部分。

我在哪里遗漏了什么?有什么办法可以实现我想要做的事情吗?非常感谢您的帮助。

【问题讨论】:

    标签: uwp uwp-xaml


    【解决方案1】:

    DataContext="{Binding RelativeSource={RelativeSource Self}}" 替换为Name="uc" 并使用ElementName 绑定到属性:

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

    那么UserControl 应该像预期的那样继承它的DataContext,但是您仍然可以在其 XAML 标记中绑定到控件本身的属性。

    【讨论】:

    • 它完成了这项工作。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 2011-01-04
    • 2019-09-28
    相关资源
    最近更新 更多