Is there a semantic difference between element property syntax and atrribute attribute syntax?

I thought that the element property syntax and attribute property syntax did not make much semantic difference. However, I found that there should be some difference.

eg. The following example simply demonstrates a simple trigger:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button><Button.Template>
  <ControlTemplate TargetType="{x:Type Button}">
    <TextBlock x:Name="hello" Text="Hello" />
    <ControlTemplate.Triggers>
      <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Foreground" Value="Red" TargetName="hello"/>
     </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>
</Button.Template></Button>
</Page>

However, if I used the element property syntax for the Propertytrigger property , it throws an exception saying that setter! (not a trigger) requires both a property and a value.

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button><Button.Template>
  <ControlTemplate TargetType="{x:Type Button}">
    <TextBlock x:Name="hello" Text="Hello" />
    <ControlTemplate.Triggers>
      <Trigger Value="True">
        <Trigger.Property>IsMouseOver</Trigger.Property>
        <Setter Property="Foreground" Value="Red" TargetName="hello"/>
     </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>
</Button.Template></Button>
</Page>

So what is the hidden difference between element property syntax and attribute property syntax?

+3
source share
1 answer

. , XAML.

Setter, Trigger Condition. Trigger.ReceiveTypeConverter Reflector, Value Property. , , Value , Property. , "" , , , "Foreground" "Foreground" Brush.

, Value Property Trigger, . , :

public class Test
    : MarkupExtension
{
    public DependencyProperty Property { get; set; }
    public DependencyProperty Property2 { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return Property ?? Property2;
    }
}

XAML , , , , :

<Trigger.Property>
    <local:Test>
        <local:Test.Property>IsMouseOver</local:Test.Property>
    </local:Test>
</Trigger.Property>

, "":

<Trigger.Property>
    <local:Test>
        <local:Test.Property2>IsMouseOver</local:Test.Property2>
    </local:Test>
</Trigger.Property>

, :

<Trigger.Property>
    <local:Test Property="IsMouseOver"/>
</Trigger.Property>

, : MarkupExtension, DependencyProperty, - , "", ProvideValue.

+2

Source: https://habr.com/ru/post/1755223/


All Articles