I'm very new to C# and XAML. I'm trying to do a WPF project where there will be a good amount of data binding. Right now, I am able to do one way data binding without any problems, the issue I am facing is when I try to do two-way data binding.
This is the beginning of my Xaml file where I try to bind a text box to a static property in a static class:
<Window x:Class="interactive_fountain.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:interactive_fountain"
xmlns:include="clr-namespace:interactive_fountain.Include"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBox x:Name="ip_textBox" HorizontalAlignment="Left" Height="27" Margin="250,242,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="110" Text="{Binding Source={x:Static include:Communication.ipAddressServer}, Path=include:Communication.ipAddressServer, UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="221,131,0,0" VerticalAlignment="Top" Height="47" Width="139" Click="Button_Click_1"/>
...
This is the beginning of the C# MainWindow Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
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;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
using interactive_fountain.Include;
namespace interactive_fountain
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Trace.WriteLine("ip: " + Communication.ipAddressServer);
}
...
And this is the beginning of the class I want to do data binding with:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
namespace interactive_fountain.Include
{
public static class Communication
{
public static string ipAddressServer = "hello";
...
WPF data binding works on public properties only, your ipAddressServer
is a static field (aka class variable) and not a property, so it won't be used. It's also failing at following proper naming conventions.
Your mess of a binding is the old style static binding, use {Binding Path=(w:Communication.IpAddressServer)}
instead (after you fix #1, of course). w
is the relevant XAML namespace definition.
Static properties don't have standard notifications for change, since static classes can't implement interfaces (for what I hope are obvious reasons). Instead WPF uses a convention-based approach of using a public static event PropertyChangedEventHandler StaticPropertyChanged
and call that to notify changes. It's not clear if you want your property to be mutable, but you mentioned the change notification mechanism explicitly, so just throwing it out there.