gpt4 book ai didi

c# - UWP 用户控件 DependencyProperty 不工作

转载 作者:太空宇宙 更新时间:2023-11-03 22:34:19 24 4
gpt4 key购买 nike

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

我定义了一个具有三个依赖属性的用户控件,用户名、密码和填充颜色。下面的代码显示了我的用户控件 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”,但颜色无效。如何解决?

更新1

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

但是还是不行。

更新 2

我更新了如下所示的 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>

但是还是不行。

最佳答案

您的绑定(bind)似乎是正确的,您的问题出在“fillcolor”属性的 Type 中。您将其绑定(bind)为字符串,而您应该将其绑定(bind)为 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绑定(bind),不支持框架元素。

关于c# - UWP 用户控件 DependencyProperty 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55910192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com