【问题标题】:Binding to a property of a list element of a model绑定到模型列表元素的属性
【发布时间】:2014-12-04 06:21:03
【问题描述】:

我有一个模型Model1,其属性为List<Model2> model2;

Model1.cs

//Other Properties
public List<Model2> model2List {get; set;}

Model2 我有这个属性Model3 model3;

Model2.cs

// Other Properties
public Model3 model3 {get; set;}

Model3.cs

// Other Properties
public string Name {get; set;}

现在我有两个用户控件View1View2View2View1 中定义

View1.xaml用户控件

<Grid x:Name="LayoutRoot">
    <!-- Some properties here bind to those of model1 and model2 -->
    <views:View2 Name="view2"></views:View2>
</Grid>

View2.xaml用户控件

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
      <phone:LongListSelector
          ItemsSource="{Binding Path=model3, Mode=OneWay}">
          <phone:LongListSelector.ItemTemplate>
              <DataTemplate>
                  <StackPanel>
                      <Border
                          BorderBrush="{StaticResource PhoneBackgroundBrush}"
                          BorderThickness="3"
                          Margin="0,12">
                          <Grid>
                          <TextBlock Text={Binding Path=Name, Mode=OneWay}>
                          </TextBlock>
                      </Border>
                      <views:View3 Name="view3">
                      </views:View3>
                  </StackPanel>
              </DataTemplate>
          </phone:LongListSelector.ItemTemplate>
      </phone:LongListSelector>    
</Grid>

我正在尝试将 View2.xaml 中的 TextBlock 绑定到 Model3 中的属性名称。在我的 CS 中,我将 DataContext 设置为

view2.DataContext = model1Object.model2List;

似乎没有工作。我还需要将view2 中定义的view3 中的控件与model3 的属性绑定。我知道这看起来太混乱了,但我被困住了。救命!

【问题讨论】:

  • 问题不清楚。
  • @EldarDordzhiev 检查现在是否有意义。
  • 把你的完整xaml
  • @MatDev8 添加了更多代码。
  • 那么,第一个问题 => 当你启动应用程序时,你有什么东西显示在屏幕上吗?你的清单上装满了物品? model3 需要是一个列表。

标签: c# xaml windows-phone-7 windows-phone-8


【解决方案1】:

那些不是属性。这些是字段。您不能绑定到字段。

使用自动属性定义将您的字段更改为属性。

public Model3 Model3 {get;set;} // PascalCase for property names, thx

这可能不是唯一的问题,但尝试绑定字段绝对是不正确的。

【讨论】:

  • 看起来这些字段是私有的。也考虑使用公共可见性:)
  • @EldarDordzhiev 哎呀!
  • 这些实际上是属性。我已经编辑了这个问题。帮助。
猜你喜欢
  • 1970-01-01
  • 2011-09-01
  • 1970-01-01
  • 2013-05-01
  • 2018-11-24
  • 1970-01-01
  • 2013-07-01
  • 2011-11-12
  • 1970-01-01
相关资源
最近更新 更多