【问题标题】:Bind to some embedded object properties绑定到一些嵌入的对象属性
【发布时间】:2013-06-24 21:12:55
【问题描述】:

我有一辆汽车IEnumerable:

public IEnumerable<ICars> Cars { get; private set; }

对象“Car”实际上包含对另一个 Ibrand 类型对象的引用:

public interface ICar
{
    // Parent brand accessor
    IBrand Brand { get; set; }

    // Properties
    int CarId { get; set; }
    string CarName { get; set; }
}

IBrand 接口有一些属性:

public interface IBrand
{
    // Properties
    string LogoName { get; set; }
    string Sector { get; set; }
}

我正在将此 Cars IEnumerable 绑定到 DataGrid - 这非常好

<DataGrid Name="Cars"
          ItemsSource="{Binding}"
          SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}, AncestorLevel=2}, Path=SelectedCar, Mode=TwoWay}">
  <DataGrid.Columns>
    <DataGridTextColumn Header="Car Id"
                        Binding="{Binding CarId}"
                        IsReadOnly="True" />
    <DataGridTextColumn Header="Car Name"
                        Binding="{Binding CarName}"
                        IsReadOnly="True" />
  </DataGrid.Columns>

我的问题是我还想绑定品牌的一些属性(所有汽车通用),例如徽标。 这个合成器不起作用

<DataGridTextColumn Header="Logo Name"
                    Binding="{Binding Brand.LogoName}"
                    IsReadOnly="True" />

你知道怎么做吗?谢谢!

【问题讨论】:

    标签: c# wpf binding


    【解决方案1】:

    当您使用虚线路径时,您需要明确指定 Path 关键字。这应该有效:

    <DataGridTextColumn Header="Logo Name" Binding="{Binding Path=Brand.LogoName}" IsReadOnly="True" />
    

    【讨论】:

    • 谢谢,现在可以使用了!在这种情况下,将在任何地方添加“路径”,与寻找不丢失的位置相比,获得 4 个字符毫无意义!干杯
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-05
    • 2011-04-17
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多