【问题标题】:why property of actual class in ObservableCollection is not found for DataGridTextcolumn but parent class property is?为什么在 DataGridTextcolumn 中找不到 ObservableCollection 中实际类的属性,但父类属性是?
【发布时间】:2016-09-07 08:06:45
【问题描述】:

我明白了:

System.Windows.Data 错误:40:BindingExpression 路径错误:“状态” 在“对象”“PointNetObject”(HashCode=9270846)上找不到属性。 绑定表达式:路径=状态; DataItem='PointNetObject' (哈希码=9270846);目标元素是'TextBlock'(名称='');目标 属性是“文本”(类型“字符串”)

一栏。所有行都添加到表中,但是对于每一行,我都会收到错误消息。每一行都显示了另外两列。但不是国家。自然不会调用转换器。

设置如下所述。

我有一个对象:

public class PointNetObject : NetObject
{
    SwitchObjectState State
    {
        get { return _state; }
        set { _state = value; }
    }
}

继承具有 Phase 和 Label 等属性的类

在 ViewModel 中:

public ObservableCollection<PointNetObject> SelectedSwitchItems { get; private set; }

public SelectedObjectsViewModel(SelectedObjects selectedObjects)
{
    SelectedSwitchItems = new ObservableCollection<PointNetObject>(GetSwitches());
}    


IEnumerable<PointNetObject> GetSwitches()
{
    foreach (var netObject in SelectedObjectsInstance.GetSelectedObjectItems(x => IsSwitch(x)))
    {
        yield return (PointNetObject) netObject;
    }
}

在视图中:

 <DataGrid Name="SelectedSwitchesGrid" CanUserAddRows="False" CanUserResizeColumns="True" CanUserSortColumns="True" IsReadOnly="True" 
                   ItemsSource="{Binding SelectedSwitchItems}"
           <DataGrid.Columns>
                    <DataGridTextColumn Header="{DynamicResource XpStrLabel}" Binding="{Binding Label}" />
                    <DataGridTextColumn Header="{DynamicResource XpStrPhase}" Binding="{Binding Phase}" />
                    <DataGridTextColumn  Header="{DynamicResource XpStrState}" Binding="{Binding State, Converter={StaticResource SwitchObjectStateToStringConverter}}" />
                </DataGrid.Columns>
  </DataGrid>

【问题讨论】:

  • 你的SwitchObjectState State 属性不是public,这非常重要。
  • 谢谢,我是个白痴
  • @Jonesopolis: 愿意写一个 asnwer 以便我能接受吗?

标签: c# wpf xaml mvvm data-binding


【解决方案1】:

在您的模型中:

public class PointNetObject : NetObject
{
    SwitchObjectState State
    {
       get { return _state; }
       set { _state = value; }
    }
}

您的State 属性未声明为公开的。

您从 ViewModel 绑定到 View 的任何属性都必须是公共的,否则 View 将无法看到它。

别担心,我们都做过一次。

【讨论】:

  • 您可能希望在您的回答中公开 State 财产:/
  • 好吧,我指出这是问题所在,也许现在更有意义
  • 这很奇怪。通常,答案包含 OP 代码的固定版本,而不是完全相同的代码并描述了错误的原因。无论如何,在给出了我不想要的意见后,我离开了。先生,你好。
  • 谢谢!通常我在 UI 中的课程是由我制作的。这个带有 State 的类是旧的,即使它已经使用了多年,财产获取者的隐私也是错误的。
猜你喜欢
  • 1970-01-01
  • 2016-12-01
  • 2022-10-20
  • 1970-01-01
  • 2018-07-31
  • 1970-01-01
  • 2021-11-08
  • 2015-03-12
  • 1970-01-01
相关资源
最近更新 更多