Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wpf binding code behind

Tags:

c#

binding

wpf

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.

<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);
like image 575
Armen Khachatryan Avatar asked Dec 08 '22 22:12

Armen Khachatryan


1 Answers

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.

like image 159
KyorCode Avatar answered Dec 26 '22 08:12

KyorCode