相当于XAML的C# [英] C# equivalent of XAML

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

问题描述

<DataGrid.Columns>
                <DataGridTextColumn Header="AuthorID" FontWeight="Black" Foreground="Red"

                                       FontSize="16" FontStyle="Italic" 

                                       CanUserReorder="True"  

                                       CanUserSort="True" SortDirection="Ascending">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="TextAlignment" Value="Center" />
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                    <DataGridTextColumn.EditingElementStyle>
                        <Style TargetType="TextBox">
                            <Setter Property="TextWrapping" Value="Wrap" />
                            <Setter Property="AcceptsReturn" Value="true" />
                        </Style>
                    </DataGridTextColumn.EditingElementStyle>

                <DataGridTemplateColumn Header="DOB" MinWidth="100" CanUserSort="False">
                    <DataGridTemplateColumn.HeaderStyle>
                        <Style TargetType="{x:Type DataGridColumnHeader}">
                            <Setter Property="Background" >
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="1,.1" StartPoint="0.5,1" SpreadMethod="Pad">
                                        <GradientStop Color="Orange" Offset="1" />
                                        <GradientStop Color="Thistle" Offset="0" />
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Foreground" Value="Black" />
                            <Setter Property="FontSize" Value="20" />
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                            <Setter Property="Width" Value="Auto" />
                            <Setter Property="BorderBrush" Value="yellow"/>
                            <Setter Property="BorderThickness" Value="2,2,2,2"/>
                        </Style>
                    </DataGridTemplateColumn.HeaderStyle>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <DatePicker SelectedDate="{Binding DOB}" SelectedDateFormat="Short" CalendarStyle="DynamicResource CalenderControlTemplate" removed="Yellow" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding DOB, StringFormat=d}" Foreground="DarkTurquoise" HorizontalAlignment="Center" />
                        </DataTemplate>
                    </dg:DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridCheckBoxColumn Header="Pass/Fail" Binding="{Binding Pass}">
                    <DataGridColumn.HeaderStyle>
                        <Style TargetType="{x:Type dg:DataGridColumnHeader}">
                            <Setter Property="Background" >
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="1,1" StartPoint="0.5,0" SpreadMethod="Pad">
                                        <GradientStop Color="#FFC2D6F6" Offset="0" />
                                        <GradientStop Color="White" Offset="1" />
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Foreground" Value="Black" />
                            <Setter Property="FontSize" Value="20" />
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                            <Setter Property="Width" Value="Auto" />
                            <Setter Property="BorderBrush" Value="Yellow"/>
                            <Setter Property="BorderThickness" Value="2,2,2,2"/>
                        </Style>
                    </DataGridColumn.HeaderStyle>
                </DataGridCheckBoxColumn>

</DataGrid.Columns>



此代码在设计时提供了一个textblock列,calender type列和checkbox列.我的问题是我如何使用代码隐藏在运行时生成相同的情况.实际上,我需要与上述代码等效的C#.

任何帮助,我们感激不尽

在此先感谢

Iqbal



This code gives a textblock column, calender type column ,and check box column at the design time . my question is how i can generate the same situation at run time using codebehind. in fact I require C# equivalent of above code .

any help is appreciated

Thanks in advance

Iqbal

推荐答案

如果您访问XAML中每个类和属性的MSDN文档,则将看到C#等效项.很容易自己弄清楚.请参见此处 [
If you go to the MSDN documentation for each class and property in your XAML, then you will see the C# equivalent. Much of it is easy to figure out for yourself. See here[^] for a sample.


在您项目的obj文件夹中,您的xaml文件将存在一个* .g.cs文件-您正在寻找的大多数内容都可能存在. >
要在运行时添加列,请执行以下操作:
Inside the obj folder of your project there will be a *.g.cs file for your xaml file - Most of what you are looking for will proably be there.

To add a column at runtime do the following:
DataGridTextColumn textColumn = new DataGridTextColumn();  
dataColumn.Header = "First Name";  
dataColumn.Binding = new Binding("FirstName");  
dataGrid.Columns.Add(textColumn);



更新



Update

DataGridTemplateColumn col = new DataGridTemplateColumn(); 
col.Header = "Date"; 
 
// Create a factory. This will create the controls in each cell of this 
// column as needed. 
FrameworkElementFactory factory = 
    new FrameworkElementFactory(typeof(DatePicker)); 
 
// Bind the value of this cell to the value of the Date property of the 
// DataContext of this row. The StringFormat "d" will be used to display 
// the value. 
Binding b = new Binding("Date"); 
b.StringFormat = "d"; 
factory.SetValue(DatePicker.SelectedDateProperty, b); 
 
// Create the template itself, and add the factory to it. 
DataTemplate cellEditingTemplate = new DataTemplate(); 
cellEditingTemplate.VisualTree = factory; 
 
col.CellEditingTemplate = cellEditingTemplate; 



最好的问候
Espen Harlinn



Best regards
Espen Harlinn


对此的最佳解决方案是
best solution for this is
string xaml="http://Schemas.microsoft.com//winfx//2006//xaml//presentation"+
            "xmlns:x=http://Schemas.microsoft.com//winfx//2006//xaml"+
           "<ur xaml code>";

XmlReader xml =XmlReader.Create(new StringReader(xaml));
UIElement element =(UIElement)XamlReader.Load(xml);

<basecontrol>.Children.Add(element);
</basecontrol></xaml>



这可以用来在运行时运行任何xaml代码



this can b used to run any xaml code at runtime


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

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