BINDING mit statischen Eigenschaften

 

 

MainWindow.xaml:

<Window x:Class="DataBindingStatisch.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataBindingStatisch"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<TextBlock Text="{Binding Path=(local:Benutzer.BenutzerName)}" />
<Button Content="Benutzer-Namen anzeigen ..." Click="Button_Click" />
</StackPanel>
</Grid>
</Window>

 

MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DataBindingStatisch
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
Benutzer.BenutzerName = "Max Mustermann";
}
}
}

 

Benutzer.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataBindingStatisch
{
public static class Benutzer
{
private static string _benutzername { get; set; }

public static string BenutzerName
{
get { return _benutzername; }
set
{
_benutzername = value;

OnStaticPropertyChanged("BenutzerName");

}
}

public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;

private static void OnStaticPropertyChanged(string propertyName)
{
var handler = StaticPropertyChanged;

if (handler != null)
{
handler(null, new PropertyChangedEventArgs(propertyName));
}
}
}
}

Top