简体   繁体   中英

How to bind button content to the static readonly field

I have a class MainWindow.xaml.cs :

namespace HomeSecurity {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged {
        public static readonly string START = "start", RESET = "RESET";
     .....
}

There is a button in MainWindow.xaml :

<Button x:Name="AcceptCamerasButton"  Content="{x:Static local:MainWindow.START}" Grid.Row="1" Click="AcceptCamerasButton_Click"></Button>

How to set content of that button to MainWindow.Start ? Current version does not work.

EDIT: I have declared:

  xmlns:local="clr-namespace:HomeSecurity" 

but still when I use:

 <Button x:Name="AcceptCamerasButton"  Content="{x:Static local:MainWindow.START}" Grid.Row="1" Click="AcceptCamerasButton_Click"></Button>

I get:

Error   1   The member "START" is not recognized or is not accessible.  

You cannot bind to fields. Bindings only work on properties. So you can either change the definition of START to a property, or create a property wrapper that returns the value of START and bind to that instead.

public static string START
{ 
   get { return "start"}
}

public static string RESET
{ 
   get { return "RESET"; }
}

Or, if you prefer to keep the readonly backing field:

private static readonly string startField = "start";

public static string START
{ 
   get { return startField}
}

Also, I'm assuming that you've already done this, but I'm including this anyway, make sure you include the namespace declaration in the XAML file for the local namespace to point to the local assembly and appropriate namespace.

xmlns:local="clr-namespace:YourProjectAssemblyName..."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM