C#中的等效代码 [英] Equivalent code in C#

查看:91
本文介绍了C#中的等效代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xaml代码。我想转换成c#。

因为我想动态完成这项任务。

 <   GridViewColumn    标题  = 员工 >  
< GridViewColumn.CellTemplate >
< DataTemplate >
< ComboBox ItemsSource = {Binding Employees} SelectedItem = {Binding
SelectedEmployee}
/ >
< / DataTemplate >
< / GridViewColumn.CellTemplate >
< / GridViewColumn >





我的尝试:



ComboBox gg = new ComboBox();

GridViewColumn mm = new GridViewColumn(){CellTemplate = gg};

解决方案

您好,请使用以下代码,

GridViewColumn objColumn = new GridViewColumn();
objColumn.Header = 员工;

DataTemplate GVCellTemplate = new DataTemplate();

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

GVCellTemplate.VisualTree = objComboBox;

objColumn.CellTemplate = GVCellTemplate;

YourGridView.Columns.Add(objColumn);


您可以看到任何有效的XAML如何转换为您使用WPF的C#或任何其他.NET语言。 XAML实际上被翻译成项目的编程语言并保存在项目的目录下。



只需用XAML创建一些代码,编译它,然后找到自动生成的文件,看看它是如何工作的。学习正在发生的事情很简单,有时非常有用。



-SA


I have a xaml code . I want to convert into c#.
Because i want to done in this task Dynamically.

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

解决方案

Hi, use the following code,

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


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


这篇关于C#中的等效代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆