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

问题描述 投票:2回答:1

我正在学习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>

我已经定义了一个具有三个依赖项属性的用户控件,用户名,密码和fillcolor。以下代码显示了我的用户控件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>

和代码隐藏:

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

用户名和密码正常工作,因为我可以在屏幕上看到“aaa”和“bbb”,但颜色不起作用。怎么解决?

UPDATE1

我将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);

但它仍然无法正常工作。

UPDATE2

我更新了下面显示的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>

但它仍然无法正常工作。

c# .net xaml xamarin uwp
1个回答
3
投票

您的绑定似乎是正确的,您的问题是在'fillcolor'属性的Type中。您将它绑定为字符串,而您应该将其绑定为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);

Fill属性需要一个Brush正常工作,你可以在这里看到Shape.Fill

我忘了提到你不需要在这种情况下指定元素名称,因为你使用你的代码作为'ViewModel',所以,下面的代码将为你做到这一点:

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

此外,您需要将页面资源中的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>

并且,根据difference-between-binding-and-xbind我们需要使用x:Bind Binding不支持框架元素。

© www.soinside.com 2019 - 2024. All rights reserved.