1

I want to dynamically change TextBlock text in my Class.

XAML-Code:

<TextBlock Name="Footer_text"  Text=""/>

C#:

string footerMainMenuText = "Setting";
Binding  set = new Binding("");
set.Mode = BindingMode.OneWay;
set.Source = footerMainMenuText;
Footer_text.DataContext = footerMainMenuText;
Footer_text.SetBinding(TextBlock.TextProperty, set);

I checked last line, and the Footer_text.Text is set correctly. ( Footer_text.Text="Setting"), but TextBlock in my application doesnt show "Setting". What is the problem here?

1
  • Have you tried removing the Text="" from the XAML? Commented Jun 30, 2012 at 13:29

1 Answer 1

5

If you are binding - why not just do it in XAML instead? Looking at your code it's kind of pointless - you might as well just go

Footer_text.Text = "Setting";

You should ideally do it in XAML or at least provide something for it to bind to

<TextBlock Text="{Binding SomeProperty}" />

I'm not sure why you would bind a 'string' on it's own to anything...do you have an object which you need to bind to the text property?

Also using

Binding("")

What does that do? A blank path? Not sure what the binding target would be there... have you tried

Binding()

instead?

Edit:

Also the reason why your binding is not updating the control, is probably because you haven't bound to an object which implements INotifyPropertyChanged or a similar interface. The controls need to know when values have changed, so I'd imagine that binding to 'string' isn't giving the TextBlock the proper notification when it changes

Edit 2:

Here is a quick example of binding working:

My window class Window.cs:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
        <TextBlock x:Name="txtName" Text="{Binding Name}"></TextBlock>
            <Button Click="Button_Click">Click me 1</Button>
            <Button Click="Button_Click_1">Click me 2</Button>
        </StackPanel>
    </Grid>
</Window>

The code behind in Window.xaml.cs

public partial class MainWindow : Window
{
    SomeObjectClass obj = new SomeObjectClass();
    public MainWindow()
    {
        InitializeComponent();

        txtName.DataContext = obj;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        obj.Name = "Hello World";
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        obj.Name = "Goobye World";
    }
}

The object to bind to (with INotifyPropertyChanged)

class SomeObjectClass : INotifyPropertyChanged
{
    private string _name = "hello";
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
            OnPropertyChanged("Name");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string PropertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
    }
}

Clicking the buttons changes SomeObject.Name, but it updates the textbox.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks,your code work great. but for my application doestn work correctly. I have three class: (Window)MainWindow, (class)SomeObjetcClass and (Window)test. SomeObjectclass is like your class. Move this two Buttons from MainWindow to test. MainWindows included only TextBlock. then it doestn work. if i click Button click me 1 from test-class, the text hello doesnt change. what should i change in your code so that control textblock from another class(test.cs) by clicking button in this class?
It sounds like you need some tuition - are you new to programming/c#? Basically - if you want to change the value from test.cs, you need to make the 'obj' variable public and pass the MainWindow instance to test.cs so you can reference the obj variable. I think doing this is maybe outside the scope of what StackOverflow is for :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.