繁体   English   中英

XAML 设置属性的属性

[英]XAML setting property of property

在 XAML 中,有没有办法设置属性的属性?

下面是一个例子来说明:

public class SomeControl : Control
{
    public SomeType SomeProperty { get; } = new SomeType(); // Property deliberately read only 
}

public class SomeType
{
    public string Name { get; set; }
}

在下面的 XAML 中,我想设置 «SomeProperty» 属性的 «Name» 值,而不向 «SomeProperty» 添加设置器,因此不创建 «SomeType» 的新 XAML 实例。

<ns:SomeControl SomeProperty.Name ="Foo"> <!-- here is the idea, but does not compile  -->
</ns:SomeControl>

问候

如果您使 SomeControl.SomeProperty 可设置,那么您可以通过 XAML 设置名称

public class SomeControl : Control
{
    public SomeType SomeProperty { get; set; }
}

public class SomeType
{
    public string Name { get; set; }
}

XAML 可能是..

<Window ...>
    <Window.Resources>
        <ns:SomeType x:Key="someType" Name="Hello World"/>
    </Window.Resources>
    
    <Grid>
        <ns:SomeControl SomeProperty="{StaticResource someType}"/>
    </Grid>
</Window>

您的属性是只读的

如果它没有 set 属性,则无法设置它

你甚至在写: // Property deliberately read only

那么你需要设置它还是只读的?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM