Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a xaml code . I want to convert into c#.
Because i want to done in this task Dynamically.
HTML
<GridViewColumn Header="Employees">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding Employees}" SelectedItem="{Binding 
                SelectedEmployee}" />
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>


What I have tried:

ComboBox gg = new ComboBox();
GridViewColumn mm = new GridViewColumn() { CellTemplate=gg};
Posted
Updated 7-Apr-16 5:39am

Hi, use the following code,
C#
GridViewColumn objColumn = new GridViewColumn();
objColumn.Header = "Employees";

DataTemplate GVCellTemplate = new DataTemplate();

FrameworkElementFactory objComboBox = new FrameworkElementFactory(typeof(ComboBox));
objComboBox.SetBinding(ComboBox.ItemsSourceProperty, new Binding("Employees"));
objComboBox.SetValue(ComboBox.SelectedItemProperty, "SelectedEmployee");

GVCellTemplate.VisualTree = objComboBox;

objColumn.CellTemplate = GVCellTemplate;

YourGridView.Columns.Add(objColumn);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Apr-16 11:40am    
There is no need to write and show such code. The code is already generated from XAML, so anyone can look at it, for any given ZAML. Please see Solution 2.
—SA
You can see how any valid XAML translated to C# or any other .NET language you use with WPF. XAML is actually translated to the programming language of a project and saved under the project's directory.

Just create some code with XAML, compile it, and then located auto-generated file and see how it works. Simple and sometimes very useful to learn what's going on.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900