C# wpf 绑定代码后面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11951431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:49:22  来源:igfitidea点击:

wpf binding code behind

c#wpfbinding

提问by Armen Khachatryan

In code i add columns to listview successfuly. But i want add binding to column than add to listview.

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

fist is working code in xaml.

拳头是 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>

Code behind:

后面的代码:

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);

采纳答案by KyorCode

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

        controllerFactory.SetBinding(TextBox.TextProperty, itemsbinding);

Create a proper binding using the code above.

使用上面的代码创建适当的绑定。

Loads of extra properties on the binding object that can assist you.

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

回答by Armen Khachatryan

 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);