WPF为什么这个绑定不起作用?

时间:2012-09-24 15:33:46

标签: c# .net wpf data-binding

我正在尝试绑定到我的viewmodel中的属性:

 private TextActionValue _textActionVal;
    public TextActionValue TextActionVal
    {
        get { return _textActionVal; }
        set
        {
            _textActionVal = value; 
        }
    }

XAML:

<Grid Margin="0,15,15,15" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <GroupBox Grid.Row="1" BorderThickness="0">
        <TextBox Margin="0,5,0,5" AcceptsReturn="True" AcceptsTab="True" Text="{Binding TextActionValue.Text}" HorizontalAlignment="Stretch" MinHeight="100"></TextBox>
    </GroupBox>
</Grid>

最后是TextActionValue类:

public class TextActionValue : ISomeAction, INotifyPropertyChanged
{
    private String _text;
    public String Text
    {
        get { return _text; }
        set
        {
            _text = value;
            OnPropertyChanged("Text");
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}

文本属性已填充。当我踏上它时,肯定有价值。

如果我只是绑定到一个基本的字符串属性,它的工作原理。它不喜欢“TextActionValue.Text”。除非我做一些我想要避免的重构,否则这不是一个选择。

如果我在TextActionValue中放置一个断点,那么get就永远不会被击中。所以这告诉我从未创建过绑定。为什么......或者是我想做的不可能?

1 个答案:

答案 0 :(得分:2)

尝试绑定到TextActionVal.Text而不是TextActionValue.Text

您正在尝试绑定到类而不是属性。

同时在调试时检查输出窗口以查看数据绑定错误。

相关问题