gpt4 book ai didi

c# - 如何从 XAML 设置 WPF 用户控件属性?

转载 作者:行者123 更新时间:2023-11-30 13:36:45 35 4
gpt4 key购买 nike

我正在尝试通过 XAML 设置同一用户控件的多个实例的填充属性,以便区分它们。我在控件的 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>

但矩形仍然未填充,即使在我运行项目时也是如此。有人可以帮忙吗?

干杯,

蒂姆

最佳答案

您的依赖属性有两处错误。

首先,它的类型应该是 Brush,而不是字符串,因为这是 WPF 控件属性使用的类型,例如 Shape.FillControl.Background 。 WPF 提供从 XAML 中的“Red”或“#FFFF0000”等字符串到 Brush 类型的自动类型转换。

其次,除了在 CLR 包装器的 setter 方法中调用 SetValue 之外,您不应该有其他任何东西。 XAML Loading and Dependency Properties 中解释了原因MSDN 上的文章:

Because the current WPF implementation of the XAML processor behavior for property setting bypasses the wrappers entirely, you should not put any additional logic into the set definitions of the wrapper for your custom dependency property. If you put such logic in the set definition, then the logic will not be executed when the property is set in XAML rather than in code.

所以你的依赖属性声明应该是这样的:

public static readonly DependencyProperty FillBrushProperty =
DependencyProperty.Register(
"FillBrush", typeof(Brush), typeof(RectangleFillTest));

public Brush FillBrush
{
get { return (Brush)GetValue(FillBrushProperty); }
set { SetValue(FillBrushProperty, value); }
}

要对属性更改使用react,您现在需要使用属性元数据注册一个 PropertyChangedCallback。但您不需要在此处执行此操作,因为您可以像这样在 UserControl 的 XAML 中简单地绑定(bind)属性:

<Rectangle Fill="{Binding FillBrush,
RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}" ... />

关于c# - 如何从 XAML 设置 WPF 用户控件属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30215659/

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