WPF - Binding problem

Posted by Erez on Stack Overflow See other posts from Stack Overflow or by Erez
Published on 2010-12-21T08:26:29Z Indexed on 2010/12/21 8:54 UTC
Read the original article Hit count: 567

Filed under:
|

Why after clicking the button the text in the textblock doesn't change ?

XAML:

 <Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <TextBlock Text="{Binding Name}"/>
    <Button Click="Button_Click" Grid.Row="1" Margin="20">Click Me</Button>
</Grid>

Code behind:

  public partial class Window1 : Window, INotifyPropertyChanged
{
    private Person _myPerson;
    public Person MyPerson
    {
        get { return _myPerson; }
        set
        {
            _myPerson = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("MyPerson"));
            }
        }
    }

    public Window1()
    {
        MyPerson = new Person { Name = "A" };
        DataContext = MyPerson;
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MyPerson = new Person { Name = "B" };
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

© Stack Overflow or respective owner

Wpf binding problem

Posted by Erez on Stack Overflow See other posts from Stack Overflow or by Erez
Published on 2010-12-21T08:46:38Z Indexed on 2010/12/21 8:54 UTC
Read the original article Hit count: 567

Filed under:
|
|
|

I have in my window a rectangle with a tooltip, Clicking the button suppose to change the tooltip text but it doesn't.

XAML:

 <Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <Grid.Resources>
        <ToolTip x:Key="@tooltip">
            <TextBlock Text="{Binding Name}"/>
        </ToolTip>
    </Grid.Resources>

    <TextBlock Text="{Binding Name}" Background="LightCoral" />
    <Rectangle Width="200" Height="200" Fill="LightBlue" VerticalAlignment="Center" HorizontalAlignment="Center" ToolTip="{DynamicResource @tooltip}" Grid.Row="1"/>
    <Button Click="Button_Click" Grid.Row="2" Margin="20">Click Me</Button>
</Grid>

code behind:

public partial class Window1 : Window
{

    public Window1()
    {
        DataContext = new Person { Name = "A" };
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        DataContext = new Person { Name = "B" };
    }
}

© Stack Overflow or respective owner

Related posts about wpf

Related posts about binding