绑定XAML中ItemsControl之外的属性 [英] Bind a Property that is outside of an Itemscontrol in XAML

查看:70
本文介绍了绑定XAML中ItemsControl之外的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绑定ItemsControl之外的Property。
但这似乎不起作用。

I am trying to bind a Property that is outside of an Itemscontrol. However that doesn't seem to work.

似乎在ItemsControl中,DataTemplate引用的是集合内部的内容,而不是集合外部的内容。
我已经尝试过使用RelativeResource并为ViewModel引用了AncestorType。

It seems that in ItemsControl, DataTemplate it refers to what is inside of the collection and not outside of it. I have tried with RelativeResource and Referred to AncestorType for the ViewModel.

代码(VM):

public class Test {
  public string GetThis {get{return "123";} set{}}
  public List<string> IterateProperty {get; set;}
}

XAML(视图):

<ItemsControl ItemsSource="{Binding Path=IterateProperty}">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <TextBlock Text="I want to bind the string property GetThis!" />


推荐答案

您需要绑定到 ItemsControl 的> DataContext 。

You need to bind to the DataContext of the parent ItemsControl.

<ItemsControl ItemsSource="{Binding Path=IterateProperty}">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding DataContext.GetThis,
                                RelativeSource={RelativeSource Mode=FindAncestor,
                                                               AncestorType={x:Type ItemsControl}}}" />

这篇关于绑定XAML中ItemsControl之外的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆