Resources.DataTemplate和ListBox.DataTemplate之间的绑定 - 的Windows Phone [英] Binding between Resources.DataTemplate and ListBox.DataTemplate - Windows Phone

查看:149
本文介绍了Resources.DataTemplate和ListBox.DataTemplate之间的绑定 - 的Windows Phone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题做Windows Phone中的绑定。希望你能帮助我。

我有以下的数据模板在App.xaml中:


  

< Application.Resources> 结果
  <&DataTemplate的GT; 结果
  <文本框名称=TXT1/> 结果
  <文本框名称=TXT2/> 结果
  < / DataTemplate中> 结果
  < /Application.Resources>


我有以下数据模板的列表框:


  

<&列表框GT; 结果
  < ListBox.ItemTemplate> 结果
  <&DataTemplate的GT; 结果
  <文本框名称=TXT1/> 结果
  <文本框名称=TXT2/> 结果
  < / DataTemplate中> 结果
  < ListBox.ItemTemplate> 结果
  <&列表框GT;


列表框会收到以下级的ItemsSource属性:

 公共类产品
{   私人诠释_id;
   公众诠释标识
   {
       {返回_id; }
       集合{_id =价值; }
   }   私人字符串_name;   公共字符串名称
   {
       {返回_name; }
       集合{_name =价值; }
   }}

反正到Resources.TextBox.Text属性与ListBoxItem中的对象绑定像...


  

< Application.Resources> 结果
  <文本框名称=TXT1文本= {绑定的ElementName =列表框,路径= SelectedItem.Product.Name} /> 结果
  < /Application.Resources>



解决方案

在最后,我无法绑定直通XAML的属性,但我做到了直通code。

我有DataTemplate中的CustomMessageBox。所以,我得到了CustomMessageBox里面的文本框与我创建了一个方法:

 公共牛逼SearchControl< T>(DependencyObject的父母,串nameControl)
        其中T:DependencyObject的
    {        如果(家长== NULL || string.IsNullOrEmpty(nameControl))
            返回null;        如果(家长T&放大器;及((FrameworkElement的)母公司).Name点== nameControl)
            返回父为T;        INT totalControles = VisualTreeHelper.GetChildrenCount(父);        的for(int i = 0; I< totalControles;我++)
        {            VAR孩子= VisualTreeHelper.GetChild(父母,我);            与T = BuscarControl< T>(儿童,nameControl);            如果(控制!= NULL)
                返回控制;        }        返回null;    }

所以,我刚才打电话的方法和分配我想要的值:

 (SearchControl<&文本框GT;(CustomMessageBoxTXT1))文本=值。

I'm having problems to do a binding in Windows Phone. Hope you can help me.

I have the following Data Template in App.xaml:

<Application.Resources>
<DataTemplate>
<TextBox Name="txt1"/>
<TextBox Name="txt2"/>
</DataTemplate>
</Application.Resources>

I have a ListBox with the following Data Template:

<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Name="txt1"/>
<TextBox Name="txt2"/>
</DataTemplate>
<ListBox.ItemTemplate>
<ListBox>

The ListBox receives the following Class in the ItemsSource Property:

public class Product
{

   private int _id;
   public int Id
   {
       get { return _id; }
       set { _id = value; }
   }

   private string _name;

   public string Name
   {
       get { return _name; }
       set { _name = value; }
   }

}

Is there anyway to bind the Resources.TextBox.Text property with the Object of the ListBoxItem like...

<Application.Resources>
<TextBox Name="txt1" Text={Binding ElementName=ListBox, Path=SelectedItem.Product.Name}/>
</Application.Resources>

解决方案

In the end, I couldn't Bind the properties thru xaml, but I did it thru code.

I have the DataTemplate in a CustomMessageBox. So, I got the Textbox inside the CustomMessageBox with a method which I created:

    public T SearchControl<T>(DependencyObject parent, string nameControl)
        where T : DependencyObject
    {

        if (parent == null || string.IsNullOrEmpty(nameControl))
            return null;

        if (parent is T && ((FrameworkElement)parent).Name == nameControl)
            return parent as T;

        int totalControles = VisualTreeHelper.GetChildrenCount(parent);

        for (int i = 0; i < totalControles; i++)
        {

            var child = VisualTreeHelper.GetChild(parent, i);

            T control = BuscarControl<T>(child, nameControl);

            if (control != null)
                return control;

        }

        return null;

    }

So, I just called the method and assigned the value that I wanted:

(SearchControl<TextBox>(CustomMessageBox, "txt1")).Text = Value;

这篇关于Resources.DataTemplate和ListBox.DataTemplate之间的绑定 - 的Windows Phone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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