Solution to invalid WPF binding elementname mode — X: reference binding

Original text:

Solution to invalid WPF binding elementname mode — X: reference binding

Demand:

Background: grid has a TextBlock Name: T1 and a listbox, and there are two TextBlock names: T2 and T3 in the listbox

Requirement: the foreground color of T2 is bound to the foreground color of T1 (the same for other attributes) and the foreground color of T3 is bound to T2

The following code removes all unnecessary content, leaving only two binding methods

<Path>
    <Path.ToolTip>
        <ToolTip>
            <StackPanel>
                <TextBlock Name="TipsTitle"/>
                <ListBox>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Name="temp_series" 
                                Foreground="{Binding Source={x:Reference Name=TipsTitle}, Path=Foreground}"/>
                                <TextBlock 
                                Foreground="{Binding ElementName=temp_series,Path=Foreground}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </StackPanel>
        </ToolTip>
    </Path.ToolTip>
</Path>

x: Reference is a function introduced in XAML 2009, which can be regarded as a relatively early function; elementname is a function that started from the beginning of XAML. They are similar in feeling, but they have more advantages in most cases.

This article explains X: reference.

A typical example of using X: reference is:

≪ object property = {X: reference instancexname} “…/&>
where instancexname is another element with the name specified by X: name.

x: Reference is preceded by an X namespace prefix, so you can imagine that this is a XAML compilation related tag similar to X: name.

It is described in Microsoft official documents as follows:

In WPF and XAML 2006, element references are addressed by the framework-level feature of ElementName binding. For most WPF applications and scenarios, Elementname binding should still be used. Exceptions to this general guidance might include cases where there are data context or other scoring considerations that make data binding impractical and where markup compilation is not involved.
in Chinese, elementname was used in XAML 2006 Get the binding source corresponding to the element in the binding, which can be used in most cases. However, if there are different named boundaries in the binding context, the binding source may not be found by using elementname. You can use X: reference instead.

Can you read that the elementname of WPF cannot be bound successfully in ContextMenu?Try reference: X! -Walterlv understands that X: reference replaces elementname to solve the problem of naming boundary in binding.

In addition, elementname determines the name scope by looking up the visual tree or logical tree at runtime, so the performance is not so good to some extent.

https://www.codercto.com/a/31458.html

 

Similar Posts: