我正在学习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>
我定义了一个具有三个依赖项属性的用户控件,分别为Username,Password和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>
但是它仍然无法正常工作。
答案 0 :(得分: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);
“填充”属性需要一个“画笔”才能正常工作,如您在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
绑定不支持框架元素。