Reputation: 849
I have a class defined as:
class Constants
{
public static string settingsToolTip = "Settings";
}
I want to set this string for a tooltip of a button like:
<Button Name= "ButtonSettingsWindow" ToolTip="Settings" ToolTipService.ShowDuration="2000"/>
Instead of hardcoding the string "Settings" in XAML, I want it to use the string from Constants class. How can i do this in WPF XAML?
Upvotes: 3
Views: 4939
Reputation: 81313
You can access static members of class using x:Static markup extension in XAML.
<Button ToolTip="{x:Static local:Constants.settingsToolTip}"/>
Make sure you have added namespace in XAML file (local) where Constant class is declared:
xmlns:local="clr-namespace:ActualNameSpace"
Upvotes: 9
Reputation: 2639
The answer you're looking for is binding.
Example:
Code behind:
class Test
{
public string test { get { return "Settings"; } }
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
XAML:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:y="clr-namespace:WpfApplication2"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<y:Test></y:Test>
</Window.DataContext>
<Grid>
<Button Content="{Binding test}"></Button>
</Grid>
The result is a button that says "Settings" :)
Upvotes: 0