简体   繁体   English

绑定到模型的列表元素的属性

[英]Binding to a property of a list element of a model

I am having a model Model1 with a property as List<Model2> model2; 我有一个模型Model1 ,其属性为List<Model2> model2; .

Model1.cs Model1.cs

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

And in Model2 I have this property Model3 model3; Model2我有这个属性Model3 model3;

Model2.cs Model2.cs

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

Model3.cs Model3.cs

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

Now I have two User Controls View1 and View2 with View2 defined in View1 现在我有两个用户控件View1View2View2中定义View1

View1.xaml UserControl 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 UserControl 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>

I am trying to bind TextBlock in View2.xaml to Property name in Model3 . 我试图在View2.xaml绑定TextBlock的物业名称Model3 From my CS I have set DataContext as 从我的CS我已将DataContext设置为

view2.DataContext = model1Object.model2List;

Doesn't seem to be working. 似乎没有用。 Also I need to bind controls in my view3 defined in view2 with properties of model3 . 另外,我需要将view2定义的view3控件与view3属性model3 I know this looks too confusing but I am stuck. 我知道这看起来太混乱了,但是我被卡住了。 Help! 救命!

Those aren't properties. 这些不是财产。 Those are fields . 那是领域 You cannot bind to fields. 您无法绑定到字段。

Change your fields into properties by using automatic property definitions. 通过使用自动属性定义将字段更改为属性。

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

This may not be the only issue, but it's definitely incorrect to try and bind against fields. 这可能不是唯一的问题,但是尝试绑定字段绝对是不正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM