Priyank Thakkar
Priyank Thakkar

Reputation: 4852

INotifyPropertyChanged for a Textbox in style

I am trying to rename a column in WPF data grid. I am providing context-menu to user for column rename. Once user clicks on the rename from a column-header of particular column, I am applying a style to the column-header using following code and style.

private void RenameColumn_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        if (e != null)
        {
            if (e.Parameter != null)
            {
                if ((e.Parameter as DataGridColumnHeader) != null)
                {
                    this.DefaultColHeaderStyle = (e.Parameter as DataGridColumnHeader).Style;
                    this.RenamedColIndex = (e.Parameter as DataGridColumnHeader).DisplayIndex;
                    (this.grTestData.ColumnFromDisplayIndex(this.RenamedColIndex)).HeaderStyle = this.grTestData.Resources["RenameColumnHeader"] as Style;
                }
            }
        }
    }

I am binding this text-box to a properpty:

<Style x:Key="RenameColumnHeader" TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Grid FocusManager.FocusedElement="{Binding ElementName=txtBxRename}">
                                <TextBox x:Name="txtBxRename" GotFocus="txtBxRename_GotFocus" LostFocus="txtBxRename_LostFocus" KeyDown="txtBxRename_KeyDown" TextChanged="txtBxRename_TextChanged" Text="{Binding Path=NewColName,Mode=TwoWay}"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

I have implemented INotifyPropertyChanged interface for property NewColName:

public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
 public string NewColName
    {
        get
        {
            return this.newColName;
        }
        set
        {
            this.newColName = value;
            this.OnPropertyChanged("NewColName");
        }
    }

but it is not triggering the property-changed when i start typing in the text-box. I am trying to implement IDataErrorInfo for the text-box validation. Please guide me. Do let me know if you need any other information about my code.

Upvotes: 0

Views: 893

Answers (2)

Priyank Thakkar
Priyank Thakkar

Reputation: 4852

It has been solved.

Whenever we are binding a control declared within a style, we need to give a name to the window.

<Window x:Class="DataGridColumnRename.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cmd="clr-namespace:DataGridColumnRename"
    Title="MainWindow" Height="350" Width="525" Name="Me">

And in the control within the style we need to specify the ElementName Property and assign windown name (in this case it is 'Me') to it.

            <Style x:Key="RenameColumnHeader" TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Grid FocusManager.FocusedElement="{Binding ElementName=txtBxRename}">
                                <TextBox x:Name="txtBxRename" GotFocus="txtBxRename_GotFocus" LostFocus="txtBxRename_LostFocus" KeyDown="txtBxRename_KeyDown" Text="{Binding ElementName=Me, Path=NewColName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>

Then only it triggers INotifyPropertyChanged. :) :) Thanks for the help guys.

Upvotes: 0

brunnerh
brunnerh

Reputation: 185140

You probably will need to set the Binding.UpdateSourceTrigger to PropertyChanged, as for TextBox.Text it is LostFocus by default.

Upvotes: 2

Related Questions