【问题标题】:INotifyPropertyChanged for a Textbox in styleINotifyPropertyChanged 用于样式中的文本框
【发布时间】:2012-03-10 23:25:09
【问题描述】:

我正在尝试重命名 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。请指导我。如果您需要有关我的代码的任何其他信息,请告诉我。

【问题讨论】:

  • 什么是标题上下文?你给他什么?
  • 我正在将数据网格与数据表绑定,并使用数据网格的自动生成列。

标签: wpf wpfdatagrid inotifypropertychanged


【解决方案1】:

您可能需要将Binding.UpdateSourceTrigger 设置为PropertyChanged,而TextBox.Text 默认为LostFocus

【讨论】:

  • @PriyankThakkar:绑定在没有errors的情况下工作?
  • 是的,我看不到任何明显的错误。但我会再次检查并通知您。
【解决方案2】:

已经解决了。

每当我们绑定在样式中声明的控件时,我们都需要为窗口命名。

<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 属性并为其分配窗口名称(在本例中为“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。 :) :) 谢谢你们的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 2014-03-05
    相关资源
    最近更新 更多