2

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>

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I changed my property to be non static. Changed my binding to {Binding Path=(local:Application.TestVal)}. However I receive the error. "Error parsing XAML. Cannot convert string '(local:Application.TestVal)}' in attribute Path to object of type System.Windows.PropertyPath. Property path is not valid.
Is that the exact error message? Looks like you might have a stray } in your binding path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.