SilverLight在运行时生成和绑定列 [英] SilverLight Generate and Bind columns at runtime

查看:70
本文介绍了SilverLight在运行时生成和绑定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在运行时向silverlight列添加列,并执行列的绑定。



这是我在xaml中静态执行的方式

 <   sdk:DataGridTextColumn       CanUserReorder   =   True    CanUserResize   =  True    CanUserSort   =  True     标题  =  Crite riaName   宽度  =  2 *   绑定  =  {Binding Path = [CriteriaName]}    IsReadOnly   =  True      /   >  





现在我想在代码中做同样的事情,



这就是我所做的事情

  foreach  string  Col  in  lColumnNames)
{
DataGridTextColumn DGC ol = new DataGridTextColumn();
DGCol.Header = Col;


绑定lObjBinding = 绑定(Col);
lObjBinding.Mode = BindingMode.OneWay;

DGCol.Binding = lObjBinding;
GrdQuickFindResult.Columns.Add(DGCol);


}





这不起作用。我能看到的全部是DataGrid中的空行,就好像绑定没有发生一样。请检查并告诉我是否所有使用xaml完成的事情都是使用C#完成的,或者是否有一些属性留在我创建的绑定对象中设置。

Thanx

解决方案

你好

我从未尝试过这个,但我看到Binding(lObjBinding)没有Source或RelativeSource。 br />


我认为当你在XAML中添加它时,Binding会使用父/持有者的来源。在XAML中,它知道Source是DataGrid的ItemsSource中的每个项目。

当您按代码创建绑定时,您可能必须手动为绑定设置Source。

I need to add columns to silverlight columns at runtime and also perform the bindings for the columns.

This is how i do it statically in xaml

<sdk:DataGridTextColumn  CanUserReorder="True" CanUserResize="True" CanUserSort="True"  Header="CriteriaName" Width="2*" Binding="{Binding Path=[CriteriaName]}" IsReadOnly="True"  />



Now i want do the same in code behind,

here is what i have done

foreach(string Col in lColumnNames)
           {
               DataGridTextColumn DGCol=new DataGridTextColumn();
               DGCol.Header= Col;


               Binding lObjBinding = new Binding(Col);
               lObjBinding.Mode = BindingMode.OneWay;

               DGCol.Binding = lObjBinding;
               GrdQuickFindResult.Columns.Add(DGCol);


           }



This is not working.All i can see are blank rows in the DataGrid,as if the binding hasn''t happened. Pls check and tell me if all the things that i have done using xaml is done using C# as well or is there some property that is left to be set in the Binding Object that i have created.
Thanx

解决方案

Hello
I have never tried this, but I see that the Binding (lObjBinding) does not have a Source or RelativeSource.

I think the Binding use the parent/holder''s source when you add it in the XAML. In the XAML, it knows that the Source is each item in the DataGrid''s ItemsSource.
When you create the binding in by code, you may have to set Source for the Binding manually.


这篇关于SilverLight在运行时生成和绑定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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