Skip to content

Instantly share code, notes, and snippets.

@Sheepings
Created March 22, 2020 20:03
Show Gist options
  • Save Sheepings/c37c0adc4965009548105de9856337ad to your computer and use it in GitHub Desktop.
Save Sheepings/c37c0adc4965009548105de9856337ad to your computer and use it in GitHub Desktop.
INotifyPropertyChanged With Bindings
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace WPFTestApp
{
public partial class Window1 : Window
{
private Main_ViewModel main_ViewModel = new Main_ViewModel();
public Window1()
{
InitializeComponent();
DataContext = main_ViewModel;
Task Begin = new Task(() => Set_Default_Values());
Begin.Start();
}
public delegate void Delegate_Callback_To(string str_Value);
public void UpdateUI_With_NewItem(string item_Value)
{
myTextBlock.Text = item_Value;
}
private void Set_Default_Values()
{
myTextBlock.Dispatcher.Invoke(new Delegate_Callback_To(UpdateUI_With_NewItem), new string[] { "Nothing Detected" });
}
private void T0_MouseMove(object sender, MouseEventArgs e)
{
main_ViewModel.New_Value.TextBlock_Text = textBox0.Text;
}
}
public class ObservableValue : INotifyPropertyChanged
{
private string TextBlock_Value;
public string TextBlock_Text
{
get
{
if (string.IsNullOrEmpty(TextBlock_Value))
return "Default TextA";
return TextBlock_Value;
}
set
{
TextBlock_Value = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string new_Value = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(new_Value));
}
}
public class Main_ViewModel
{
public ObservableValue New_Value { get; }
public Main_ViewModel()
{
New_Value = new ObservableValue();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment