CathalMF
CathalMF

Reputation: 10055

Specify what namespace to bind a property from

I have a simple UserControl where I want to bind a Label text value to a property in a DLL. My Main Application allows me to insert my own user control by specifying the XAML.

In the below XAML the CheckSubtotal property which I am using is part of the application which loads the user control. This property populates in the control without any problems. In the below XAML the TestValue property is in MyTestDLL.dll which I have specified at the beginning of the XAML. This property is NOT populating in the control.

Example DLL - MyTestDLL.dll

namespace MyTestNamespace
{
    public class Application
    {
        public static string TestValue = "TestVal";
        public Application()
        {}
    }
}

Example XAML

<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:system="clr-namespace:System;assembly=mscorlib"
    xmlns:systemWindows="clr-namespace:System.Windows;assembly=PresentationFramework" 
    xmlns:local="clr-namespace:MyTestNamespace;assembly=MyTestDLL"
    >

    <Grid>
        <DockPanel>
            <Border DockPanel.Dock="Left" Width="294" Margin="3,0,4,3" CornerRadius="0,0,3,3" Padding="2" BorderBrush="Black" BorderThickness="0" Background="White">
                <DockPanel>
                    <StackPanel DockPanel.Dock="Bottom" HorizontalAlignment="Stretch" Margin="5,0,5,0">
                        <Grid>
                            <Label FontSize="20" Content="SubTotal:" HorizontalAlignment="Left" Margin="10,0,0,0" Width="129.51"/>
                            <Label FontSize="20" HorizontalAlignment="Right" Margin="0,0,21,0" Content="{Binding Path=CheckSubtotal}"/>
                        </Grid>
                        <Grid>
                            <Label FontSize="20" Content="TestValue:" HorizontalAlignment="Left" Margin="10,0,0,0" Width="129.51"/>
                            <Label FontSize="20" HorizontalAlignment="Right" Margin="0,0,21,0" Content="{Binding Path=TestValue}"/>
                        </Grid>
                    </StackPanel>
                </DockPanel>
            </Border>
        </DockPanel>
    </Grid>

</UserControl>

Upvotes: 2

Views: 3264

Answers (1)

Mike Strobel
Mike Strobel

Reputation: 25623

The namespace-qualified syntax for referencing a member in XAML is namespacePrefix:Type.Member, or in your case, local:Application.TestValue. Note that when using a qualified member name in a PropertyPath, the member should be enclosed in parentheses, e.g., {Binding Path=(ns1:A.B).(ns2:C.D)}.

However, since you are "binding" to a static member, you can simply use {x:Static local:Application.TestValue}. WPF 4.0 and earlier do not support binding to static members, so x:Static is the only way to go, but keep in mind that x:Static only resolves once and does not observe changes to the referenced value.

Upvotes: 5

Related Questions