2012-08-14 68 views
4

在代碼中,我將列添加到listview成功。但我想添加綁定到列而不是添加到列表視圖。拳頭是xaml中的工作代碼。後面的wpf綁定代碼

<GridViewColumn x:Name="colName" Header="Name" Width="130"> 
    <GridViewColumn.CellTemplate> 
    <DataTemplate> 
     <TextBlock Text="{Binding Path=Values, Converter={StaticResource LoadProfileConverter},ConverterParameter=active_total}"/> 
    </DataTemplate> 
    </GridViewColumn.CellTemplate> 
</GridViewColumn> 

代碼後面:

GridViewColumn column = new GridViewColumn(); 
column.Header = "Header"; 
column.Width = 130;      

FrameworkElementFactory controlFactory = new FrameworkElementFactory(typeof(TextBlock)); 

var itemsBinding = new System.Windows.Data.Binding("Values") 
{ 
    Converter = new LoadProfileConverter(), 
    ConverterParameter = "active_total", 
}; 

controlFactory.SetBinding(TextBox.TextProperty, itemsBinding); 

DataTemplate template = new DataTemplate(); 
template.VisualTree = controlFactory; 

column.CellTemplate = template; 

LoadProfileGrid.Columns.Add(column); 
+2

請在發佈之前重新閱讀。 – franssu 2012-08-14 11:40:11

+0

http://msdn.microsoft.com/en-us/library/ms742863.aspx如何:在代碼中創建綁定 – ShadeOfGrey 2012-08-14 11:42:46

+0

@ShadeOfGrey但這裏沒有關於ConverterParameter(( – 2012-08-14 12:17:32

回答

14
var itemsbinding = new Binding("Values") 
      { 
       Converter = new LoadProfileConverter(), 
       ConverterParameter = key 
      }; 

     controllerFactory.SetBinding(TextBox.TextProperty, itemsbinding); 

使用上面的代碼創建一個適當的結合。

可以爲您提供幫助的綁定對象上的額外屬性負載。

+0

看起來不錯,當看到我認爲這是什麼我需要,但不是工作:( – 2012-08-14 12:07:38

+0

你可以提供更多的代碼,然後請 – KyorCode 2012-08-14 12:14:53

+0

請參閱更新請 – 2012-08-14 12:31:25

0
GridViewColumn column = new GridViewColumn(); 
          column.Header = key; 
          column.Width = 130;      

          FrameworkElementFactory controlFactory = new FrameworkElementFactory(typeof(TextBlock)); 

          var itemsBinding = new System.Windows.Data.Binding("Values") 
          { 
           Converter = new LoadProfileConverter(), 
           ConverterParameter = key 

          };       

          column.DisplayMemberBinding = itemsBinding; 
          LoadProfileGrid.Columns.Add(column); 
相關問題