Default Constructor Parameter in MarkupExtension declaration

后端 未结 1 1946
梦毁少年i
梦毁少年i 2021-02-05 15:33

Reducing this question to the bare minimum, consider this MarkupExtension class...

public class ProblemStatement : MarkupExtension
{
    private readonly string          


        
相关标签:
1条回答
  • 2021-02-05 16:04

    Try this out:

        public string Optional{ get; set; } = "DefaultValue";
    
        private readonly string _mandatory;
    
        public ProblemStatement(string mandatory)
        {
            _mandatory = mandatory;
        }
    

    Usage:

    <TextBlock Name="TextBlock1" Tag="{local:ProblemStatement 'hello', Optional=NotDefault}"/>
    

    Alternative:

    <TextBlock Name="TextBlock1" Tag="{local:ProblemStatement 'hello'}"/>
    

    Result:

    • No XAML parsing errors
    • No need to overload the constructor for optional parameters
    • Mandatory parameters are constructor parameters.
    • Optional parameters are properties.
    0 讨论(0)
提交回复
热议问题