Want to bind WPF in xaml other than behind class

Asked 2 years ago, Updated 2 years ago, 115 views

Hello
As per the title, TextBlock text in XAML is not a behind class
Is it possible to bind from any other class?

I want to rewrite TextBlock text in an XAML from any class.

c# wpf xaml

2022-09-30 15:34

1 Answers

TextBlock text in the XAML can be bound from any class.
Search "WPF DataContext Binding" to find bindings and MVVM web pages.

Will the WpfApp1.csproj sample code below match the desired behavior?
Removing the UpdateSourceTrigger in xaml causes a TextBlock rewrite when lost focus occurs.

If INotifyPropertyChanged is not written, the control will not be able to sense that the text has been changed from the source.
The sample code uses Timer to change the text from the Tick method.
Enabling the ★ code commented out in the Tick method does not change the text, but implementing the INotifyPropertyChanged event will reflect the text change from the code.

MainWindow.xaml:

<Window:Class="WpfApp1.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:WpfApp1"
        mc —Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0"Name="MyStackPanel">
            <TextBox Text="{Binding SimpleText, UpdateSourceTrigger=PropertyChanged}"/>
            <TextBlock Text="{Binding SimpleText}"/>
        </ StackPanel>
        <StackPanel Grid.Row="1">
            <TextBlock Text="{Binding NotifyText}"/>
        </ StackPanel>
    </Grid>
</Window>

MainWindow.xaml.cs:

using System.ComponentModel;
using System.Threading;
using System.Windows;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml Interaction Logic
    /// </summary>
    public partial class MainWindow—Window
    {
        private Timer MyTimer;
        private SimpleModel MySimpleModel=new SimpleModel();
        private NotifyModel MyNotifyModel=newNotifyModel();

        public MainWindow()
        {
            InitializeComponent();

            MySimpleModel.SimpleText="Enter here to rewrite the label.";
            MyStackPanel.DataContext=MySimpleModel;
            DataContext=MyNotifyModel;
            MyTimer = new Timer (Tick, null, 0, 1000);

        }

        private void Tick (object state)
        {
            // ★ SimpleModel does not change when changing values from the source (value changes are not notified to xaml)
            // MySimpleModel.SimpleText=DateTime.Now.ToString("HH:mm:ss");

            // NotifyModel changes the display when you change the value from the source.
            MyNotifyModel.Next();
        }
    }

    public class SimpleModel
    {
        public string SimpleText {get;set;}
    }

    public class NotifyModel:INotifyPropertyChanged
    {
        public string NotifyText
        {
            get {return Text;}
            set
            {
                Text = value;
                OnPropertyChanged (nameof (NotifyText));
            }
        }
        private string text;
        private int Count = 0;

        public void Next()
        {
            Count++;
            Count=(Count>5) ?0:Count;
            NotifyText=new string('■', Count);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged (string info)
        {
            if(PropertyChanged==null)return;
            PropertyChanged.Invoke (this, new PropertyChangedEventArgs(info));
            // PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info)); // C#6.0 or later, the above two lines of rewrite (Null condition operator)
        }
    }
}


2022-09-30 15:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.