wpf绑定代码背后

时间:2012-08-14 11:36:12

标签: c# wpf binding

在代码中,我成功地将列添加到listview。但我想添加绑定到列而不是添加到listview。

拳头是xaml中的代码。

<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 个答案:

答案 0 :(得分:14)

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

        controllerFactory.SetBinding(TextBox.TextProperty, itemsbinding);

使用上面的代码创建正确的绑定。

可以帮助您的绑定对象上的大量额外属性。

答案 1 :(得分: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);