假设我们有这样的控制:
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/