Johannes Schacht
Johannes Schacht

Reputation: 1354

Basics: Can't reference static member in XAML

I try to access a static member in XAML but the compiler keeps complaining. Can anyone help.

This is the XAML

<Window x:Class="tt_WPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:tt_WPF"
    Title="MainWindow" SizeToContent="WidthAndHeight">
<Button  Content="{x:Static local:MainWindow.SomeString}" />
</Window>

This is the code behind

namespace tt_WPF
{
public partial class MainWindow : Window
{
    public static string SomeString = "Hello";
    public MainWindow()
    {
        InitializeComponent();
    }
}
}

Upvotes: 1

Views: 418

Answers (1)

Will Eddins
Will Eddins

Reputation: 13907

Your code works perfectly fine as-is, with no changes.

Window with a Button that says "Hello"

If it won't let you compile, I'd recommend doing a Clean Solution and restarting Visual Studio. If that doesn't work, try creating a new WPF project and copying your code in, incase some other file is messed up.

Upvotes: 1

Related Questions