c# – 如何从XAML设置WPF usercontrol属性?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何从XAML设置WPF usercontrol属性?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2749字,纯文字阅读大概需要4分钟。
内容图文
![c# – 如何从XAML设置WPF usercontrol属性?](/upload/InfoBanner/zyjiaocheng/757/82ae7477e03d42a9b76b82112c8191d4.jpg)
我正在尝试从XAML设置同一usercontrol的几个实例的fill属性,以便区分它们.我在控件的C#代码隐藏中使用依赖属性,并在实例化控件时引用XAML中的依赖属性.这是我尝试过的简化示例,首先是用户控件的XAML:
<UserControl x:Class="RectangleFillUserControlTest.RectangleFillTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="150">
<Grid>
<Rectangle x:Name="rect" HorizontalAlignment="Left" Height="50" Stroke="Black" VerticalAlignment="Top" Width="150"/>
</Grid>
</UserControl>
现在代码隐藏:
namespace RectangleFillUserControlTest
{
public partial class RectangleFillTest : UserControl
{
SolidColorBrush fillBrush;
public static readonly DependencyProperty FillColourProperty = DependencyProperty.Register
("FillColour", typeof(string), typeof(RectangleFillTest), new PropertyMetadata(string.Empty));
public string FillColour
{
get { return (string)GetValue(FillColourProperty); }
set
{
SetValue(FillColourProperty, value);
if (value == "red") fillBrush = new SolidColorBrush(Colors.Red);
else fillBrush = new SolidColorBrush(Colors.Green);
rect.Fill = fillBrush;
}
}
public RectangleFillTest()
{
InitializeComponent();
}
}
}
我在主窗口中实例化控件并尝试将填充颜色设置为红色:
<Window x:Class="RectangleFillUserControlTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:RectangleFillUserControlTest"
Title="MainWindow" Height="350" Width="525">
<Grid Background="#FF1D2CC3">
<local:RectangleFillTest FillColour="red"/>
</Grid>
</Window>
但即使我运行项目,矩形仍未填充.有人可以帮忙吗?
干杯,
蒂姆
解决方法:
我将解释为什么不工作以及如何解决.
1.-只有当usercontrol在可视化树中具有该依赖项属性时,才会调用依赖项属性.
如果你想以这种方式做,你需要添加例如:
new PropertyMetadata(string.Empty, ValueChanged));
并改变了价值:
public static readonly DependencyProperty FillColourProperty = DependencyProperty.Register
("FillColour", typeof(string), typeof(RectangleFillTest), new PropertyMetadata(string.Empty, ValueChanged));
private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as RectangleFillTest;
var fillBrush = new SolidColorBrush();
if (control.FillColour == "red")
fillBrush = new SolidColorBrush(Colors.Red);
else
fillBrush = new SolidColorBrush(Colors.Green);
control.rect.Fill = fillBrush;
}
public string FillColour
{
get
{
return (string)GetValue(FillColourProperty);
}
set
{
SetValue(FillColourProperty, value);
}
}
这对你的逻辑是明确的,如果你需要一个更通用的代码用于任何颜色等,使用将属性绑定到矩形,请告诉我.
内容总结
以上是互联网集市为您收集整理的c# – 如何从XAML设置WPF usercontrol属性?全部内容,希望文章能够帮你解决c# – 如何从XAML设置WPF usercontrol属性?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。