簡體   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