Erre Efe
Erre Efe

Reputation: 15557

Why isn't change button content working

I have this code:

private void ModifyButton_Click(object sender, RoutedEventArgs e)
{
    ModifyButton.Content = "Another button name";
}

But it doesn't work. I mean, the modify button content doesn't change but the program doesn't fail or throw any exception.

I'm trying to modify the button name in order to change it's behavior (kinda Edit/Save) within the same button. Is this not possible using C#/WPF?

Thanks in advance.

EDIT:

XAML:

<Button Name="ModifyButton" Content="Modificar" Margin="5,10,0,0" Height="23" Width="120" HorizontalAlignment="Left" Click="ModifyButton_Click"></Button>

WEIRD BEHAVIOR: If I put a MessageBox.Show call after the change of the button content, then, while the message box is displayed the button dislay the new (changed) name, but after the message box is closed, then it shows it's original text.

Upvotes: 0

Views: 2159

Answers (2)

Jens H
Jens H

Reputation: 4632

I guess that the XAML of your UI is not bound to the value of your button. Did you check the DataBinding?

[EDIT]

Your magic information here is that you use ShowDialog(). As you already guessed, this influences your UI thread and therefore the display behavior. ShowDialog() displays the Form as a modal dialog and blocks your UI thread and therefore blocks the refresh of it. This may cause all sorts of weird behavior.

Upvotes: 1

Alex Mendez
Alex Mendez

Reputation: 5150

This is what i have and it works:

Window 1

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Button Name="ModifyButton" Content="Open Dialog" Margin="80,104,78,0" Height="23" Click="ModifyButton_Click" VerticalAlignment="Top"></Button>
    </Grid>
</Window>

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void ModifyButton_Click(object sender, RoutedEventArgs e)
    {
        Window2 win2 = new Window2();
        win2.ShowDialog();
    }
}

Window 2

<Window x:Class="WpfApplication1.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="300" Width="300">
    <Grid>
        <Button Name="ModifyButton" Content="Modificar" Margin="80,104,78,0" Height="23" Click="ModifyButton_Click" VerticalAlignment="Top"></Button>
    </Grid>
</Window>

public partial class Window2 : Window
{
    public Window2()
    {
        InitializeComponent();
    }

    private void ModifyButton_Click(object sender, RoutedEventArgs e)
    {
        ModifyButton.Content = "Another button name";
    }
}

Upvotes: 0

Related Questions