INotifyPropertyChanged用于样式的文本框

时间:2012-02-20 16:02:27

标签: wpf wpfdatagrid inotifypropertychanged

我正在尝试重命名WPF数据网格中的列。我正在为用户提供列重命名的上下文菜单。一旦用户从特定列的列标题中点击重命名,我就会使用以下代码和样式将样式应用于列标题。

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;
                }
            }
        }
    }

我正在将此文本框绑定到一个正确的内容:

<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>

我已经为属性NewColName实现了INotifyPropertyChanged接口:

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");
        }
    }

但是当我开始在文本框中输入时,它不会触发属性更改。我正在尝试为文本框验证实现IDataErrorInfo。请指导我。如果您需要有关我的代码的任何其他信息,请告诉我。

2 个答案:

答案 0 :(得分:2)

您可能需要将Binding.UpdateSourceTrigger设置为PropertyChanged,默认情况下TextBox.TextLostFocus

答案 1 :(得分:0)

它已经解决了。

每当我们绑定一个样式中声明的控件时,我们需要给窗口命名。

<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">

在样式中的控件中,我们需要指定ElementName属性并为其指定windown名称(在本例中为'Me')。

            <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>

然后只触发INotifyPropertyChanged。 :) :)感谢帮助人员。

相关问题