简体   繁体   English

UWP用户控件DependencyProperty无法正常工作

[英]UWP User Control DependencyProperty not working

I'm learning UWP User Control and facing the below code: 我正在学习UWP用户控制并面对以下代码:

<Page
    x:Class="LearningUWP.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:LearningUWP"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <StackPanel>
        <local:MyUserControl Username="aaa" Password="bbb" fillcolor="Blue" />
    </StackPanel>
</Page>

I have defined a user control with three dependency properties, Username, Password, and fillcolor. 我已经定义了一个具有三个依赖项属性的用户控件,用户名,密码和fillcolor。 The below code shows my user control xaml 以下代码显示了我的用户控件xaml

<UserControl
    x:Name="myctrl"
    x:Class="LearningUWP.MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:LearningUWP"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    >
    <RelativePanel>
        <Rectangle Fill="{Binding fillcolor, ElementName=myctrl}"  x:Name="Rec" Height="100"   RelativePanel.AlignRightWithPanel="True" />
        <TextBlock Text="Username_lbl" RelativePanel.AlignRightWithPanel="True" TextAlignment="Center" RelativePanel.Below="Rec" x:Name="Username_lb" Margin="0,50,0,0" RelativePanel.AlignLeftWithPanel="True" />
        <TextBox x:Name="Username_input" RelativePanel.Below="Username_lb" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignLeftWithPanel="True" Margin="0,20,0,0" Text="{Binding Username, ElementName=myctrl}"  ></TextBox>
        <TextBlock Text="Password_lbl" x:Name="Password_lb" RelativePanel.AlignRightWithPanel="True" TextAlignment="Center" RelativePanel.Below="Username_input" RelativePanel.AlignLeftWithPanel="True" Margin="0,20,0,0" ></TextBlock>
        <TextBox x:Name="Password_input" RelativePanel.Below="Password_lb" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignLeftWithPanel="True" Margin="0,20,0,0" Text="{Binding Password, ElementName=myctrl}" ></TextBox>
    </RelativePanel>
</UserControl>

and the code-behind: 和代码隐藏:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace LearningUWP
{
    public sealed partial class MyUserControl : UserControl
    {
        public MyUserControl()
        {
            this.InitializeComponent();
        }
        public string Username
        {
            get { return (string)GetValue(UsernameProperty); }
            set { SetValue(UsernameProperty, value); }
        }
        public static readonly DependencyProperty UsernameProperty =
            DependencyProperty.Register("Username", typeof(string), typeof(MyUserControl), null);
        public string Password
        {
            get { return (string)GetValue(PasswordProperty); }
            set { SetValue(PasswordProperty, value);         }
        }
        public static readonly DependencyProperty PasswordProperty =
            DependencyProperty.Register("Password", typeof(string), typeof(MyUserControl), null);
        public string fillcolor
        {
            get { return (string)GetValue(fillcolorProperty); }
            set { SetValue(fillcolorProperty, value); }
        }
        public static readonly DependencyProperty fillcolorProperty =
            DependencyProperty.Register("fillcolor", typeof(string), typeof(MyUserControl), null);
    }
}

The Username and Password are working as I can see "aaa" and "bbb" on my screen, but the color is not working. 用户名和密码正常工作,因为我可以在屏幕上看到“aaa”和“bbb”,但颜色不起作用。 How to solve it? 怎么解决?

UPDATE1 UPDATE1

I modified the fillcolor property to the below code: 我将fillcolor属性修改为以下代码:

public Brush fillcolor
        {
            get { return (Brush)GetValue(fillcolorProperty); }
            set { SetValue(fillcolorProperty, value); }
        }
        public static readonly DependencyProperty fillcolorProperty =
            DependencyProperty.Register("fillcolor", typeof(Brush), typeof(MyUserControl), null);

But it's still not working. 但它仍然无法正常工作。

UPDATE2 UPDATE2

I updated the MainPage.xaml shown in the below: 我更新了下面显示的MainPage.xaml:

<Page
    x:Class="LearningUWP.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:LearningUWP"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Page.Resources>
        <SolidColorBrush x:Key="MyBlueColor" Color="#0000FF"/>
    </Page.Resources>
    <StackPanel>
        <local:MyUserControl Username="aaa" Password="bbb" fillcolor="{StaticResource MyBlueColor}" />
    </StackPanel>
</Page>

But it's still not working. 但它仍然无法正常工作。

Your binding seems correct, your problem is in the Type of your 'fillcolor' property. 您的绑定似乎是正确的,您的问题在'fillcolor'属性的Type中。 You are binding it as a string, whereas you should bind it as a Brush 您将它绑定为字符串,而您应该将其绑定为Brush

 public Brush fillcolor
        {
            get { return (Brush)GetValue(fillcolorProperty); }
            set { SetValue(fillcolorProperty, value); }
        }
 public static readonly DependencyProperty fillcolorProperty =
            DependencyProperty.Register("fillcolor", typeof(Brush), typeof(MyUserControl), null);

The Fill property requires a Brush to work properly as you can see in here Shape.Fill Fill属性需要一个Brush才能正常工作,你可以在这里看到Shape.Fill

I forgot to mention that you don't need to specify the element name in this case as you are using your code behind as a 'ViewModel', so, the code below will do the trick for you: 我忘了提到你不需要在这种情况下指定元素名称,因为你使用你的代码作为'ViewModel',所以,下面的代码将为你做到这一点:

<Rectangle Fill="{x:Bind fillcolor}"  x:Name="Rec" Height="100"   RelativePanel.AlignRightWithPanel="True" />

Furthermore, you need to specify the Brush in your page's resources as a solid color brush, so you could write: 此外,您需要在页面的资源中将Brush指定为纯色画笔,因此您可以编写:

<Page
    x:Class="LearningUWP.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:LearningUWP"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Page.Resources>
     <SolidColorBrush x:Key="MyBlueColor" Color="#0000FF"/> <!-- or Blue -->
    </Page.Resources>
    <StackPanel>
        <local:MyUserControl Username="aaa" Password="bbb" fillcolor="{StaticResource MyBlueColor}" />
    </StackPanel>
</Page>

And, according to difference-between-binding-and-xbind We need to use x:Bind Binding does not support framework elements. 并且,根据绑定与xbind之间的区别我们需要使用x:Bind Binding不支持框架元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM