gpt4 book ai didi

wpf - 在 MyUserControl.xaml 中以声明方式设置 MyUserControl 的属性

转载 作者:行者123 更新时间:2023-12-04 18:30:44 26 4
gpt4 key购买 nike

假设我们有这样的控制:

public partial class MyUserControl : UserControl
{
public MyUserControl() {
InitializeComponent();
}

public string Foo { get; set; }
}

如何在 MyUserControl.xaml 中以声明方式设置“Foo”属性值?

<UserControl x:Class="Test.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<!-- Looking for some thing like this -->
<Foo>Hola</Foo>

</UserControl>

更清楚一点:如何在 XAML 中为代码隐藏中定义的属性设置值。

最佳答案

我过去发现,您可以使用样式从 xaml 设置 UserControl 上的属性,而无需继承。试试这样的:

<UserControl x:Class="Test.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:Test.MyUserControl" >

<UserControl.Style>
<Style>
<Setter Property="c:MyUserControl.Foo" Value="Hola" />
</Style>
</UserControl.Style>

</UserControl>

关于wpf - 在 MyUserControl.xaml 中以声明方式设置 MyUserControl 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/658045/

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