【问题标题】:WPF why is this binding not working? [closed]WPF 为什么这个绑定不起作用? [关闭]
【发布时间】:2012-09-24 15:33:45
【问题描述】:

我试图绑定到的视图模型中的属性:

 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 中放置一个断点...get 永远不会被命中。所以这告诉我绑定永远不会创建。为什么...或者我试图做的事情是不可能的?

【问题讨论】:

  • 您的属性名为 TextActionVal,但在您的 TextBox 的 Text 属性中,您将绑定设置为 TextActionValue
  • 为什么会有人反对这个问题?谁在乎这是否是一个简单的疏忽。你说的是两个字符的区别。
  • 因为只帮助一个人的问题是没有用的。 (学习debug data bindings...)
  • 这里 50% 的问题是为了“帮助一个人”……发帖者。
  • @user1631520:您不能从描述性陈述中推导出规范性陈述。阅读原因描述:This question is unlikely to help any future visitors。这些是规则,其他的都无关紧要。

标签: c# .net wpf data-binding


【解决方案1】:

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

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

在调试时还要检查您的输出窗口以查看数据绑定错误。

【讨论】:

  • 上帝啊...谢谢你!!!我知道这将是一个愚蠢的愚蠢错误。如果是这样,我会尝试标记为答案(我确定是这样)
  • 就是这样!已标记答案。欣赏它。 (我要等 5 分钟……但我会接受)
猜你喜欢
  • 1970-01-01
  • 2012-07-05
  • 1970-01-01
  • 2017-12-13
  • 1970-01-01
  • 2010-10-26
  • 2017-06-07
  • 1970-01-01
相关资源
最近更新 更多