WPF绑定嵌套文本框的文本不起作用

时间:2011-07-28 12:46:47

标签: c# wpf xaml data-binding

我有一个XAML代码如下:

<UserControl x:Class="LabsterApp.TestExplorer.TestExplorer"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:TestExplorer="clr-namespace:LabsterApp.TestExplorer" MinHeight="300">
    <Grid>
        <StackPanel MinWidth="120">
            <TreeView/>
            <Grid Name="TestInfo">
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <TextBox Text="Name:" Grid.Row="0" Grid.Column="0"/>
                <TextBox Text="{Binding ElementName=CurrentTestInfo, Path=TestName}" Grid.Row="0" Grid.Column="1"/>
            </Grid>
        </StackPanel>
    </Grid>
</UserControl>

我将整个控件的数据上下文设置为其viewmodel(在后面的代码中):

public TestExplorer()
        {
            viewModel = new TreeExplorerViewModel();
            DataContext = viewModel;
            InitializeComponent();
        }

viewmodel有一个CurrentTestInfo对象,它具有TestName属性(全部公开)。在某些时候,我更新CurrentTestInfo(每次创建一个新对象),但文本框中的文本不会更新。我尝试过使用:

<TextBox Text="{Binding Path=CurrentTestInfo.TestName}" Grid.Row="0" Grid.Column="1"/>  

代替并尝试了嵌套绑定(textbox \ text \ binding标签) 相关的viewmodel部分:

public void TreeNodeSelected(object selectedNode)
{
    var node = (FolderTreeNode)selectedNode;
    if (!node.IsFolder)
    {
        HandleTest(node);
        return;
    }
    HandleFolderNode(node);
}

这是一个测试节点,所以我们转到HandleTest

    // puts null/"" in all the fields:
    CurrentTestInfo.Clear();
    // locate the node in the currentData table
    DataRow currentRow = null;
    foreach (DataRow dataRow in currentData.Rows)
    {
        if (dataRow["TestID"].ToString().Equals(string.Empty)) continue;
        if (node.TestId == Guid.Parse(dataRow["TestID"].ToString()))
        {
            currentRow = dataRow;
            break;
         }
     }
     CurrentTestInfo.TestName = currentRow["TestName"].ToString();
     CurrentTestInfo.Owner = currentRow["TestOwner"].ToString();
     CurrentTestInfo.LastUpdated = DateTime.Parse(currentRow["TestDate"].ToString());

TestInfo类:

public class TestInfo : INotifyPropertyChanged
    {
        private string testName;
        public string TestName
        {
            get { return testName; }
            set
            {
                testName = value;
                OnPropertyChanged(new PropertyChangedEventArgs("TestName"));
            }
        }

        private DateTime? lastUpdated;
        public DateTime? LastUpdated
        {
            get { return lastUpdated; }
            set
            {
                lastUpdated = value;
                OnPropertyChanged(new PropertyChangedEventArgs("LastUpdated"));
            }
        }

        private string owner;
        public string Owner
        {
            get { return owner; }
            set 
            {
                owner = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Owner"));
            }
        }

        public void Clear()
        {
            TestName = string.Empty;
            Owner = string.Empty;
            LastUpdated = null;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }
    }

我做错了什么?

感谢。

2 个答案:

答案 0 :(得分:2)

在绑定中,当您想要绑定到XAML元素的属性时,使用'ElementName'。例如,如果您绑定到另一个TextBox的Text属性。

将绑定更改回:

Text="{Binding Path=CurrentTestInfo.TestName}"

此外,验证您的ViewModel实现了INotifyPropertyChanged。您的ViewModel需要引发PropertyChanged事件,以便UI控件知道自己刷新。

答案 1 :(得分:1)

您的视图模型和TestInfo类型是否实现了INotifyPropertyChanged?您是否在设置PropertyChangedCurrentTestInfo属性时调用TestName事件?