如何在C#中将DataTemplate添加到特定的GridView Column单元格 [英] How to add a DataTemplate to a particular GridView Column cell in c#

查看:73
本文介绍了如何在C#中将DataTemplate添加到特定的GridView Column单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的

我有一个WPF应用程序,不幸的是,我需要在其中将特定的DataTemplate应用于GridView列单元格.
我尝试了不同的方法,但是模板根本不适用,但是正确显示了绑定到gridview的itemSource的数据

I have a WPF application where I need , unfortunatly, to apply a particular DataTemplate to a GridView column cell.
I have try different things but the template did not apply at all, but data bind to the itemSource of the gridview are correctly displayed

列表的XAML布局如下:

The XAML layout of the list is as below :

<ListView 
                    x:Name="lvParameterList" 
                    ItemsSource="{Binding}"                     
                    ItemContainerStyle="{DynamicResource ListViewItemContainerStyle}"
                    BorderThickness="0"
                    SelectionChanged="lvParameterList_SelectionChanged" FontSize="14">
                    
                    <ListView.View>
                         <GridView 
                            x:Name="gvParameterList" 
                           ColumnHeaderContainerStyle="{DynamicResource GridViewColumnHeaderStyle}" />
                            
                    </ListView.View>
                    <ListView.Style>
                        <Style TargetType="{x:Type ListView}">
                            <Style.Triggers>
                                <Trigger Property="DataContext" Value="{x:Null}">
                                    <Setter Property="Visibility" Value="{x:Static Visibility.Collapsed}"/>
                                </Trigger>
                                <!--<DataTrigger Binding="{Binding Path=Status}" Value="0">
                                    
                                </DataTrigger>-->
                            </Style.Triggers>
                            
                        </Style>
                    </ListView.Style>
                      
                </ListView>

我在gridview列中动态构建的c#代码如下:

The c# code where I am building dynamically the gridview column is as below :

gvParameterList.Columns.Clear(); foreach (DataColumn dataCol in dt.Columns) { //if (dataCol.ColumnName != "RecordNb" && dataCol.ColumnName != "PageCount" && dataCol.ColumnName != "Status") //change on 29/10/2015 to add status field if (dataCol.ColumnName != "RecordNb" && dataCol.ColumnName != "PageCount" ) { GridViewColumn gvCol = new GridViewColumn(); gvCol.DisplayMemberBinding = new Binding(dataCol.ColumnName) { Converter = new Maillefer.Nomos.Plateform.UI.Common.DateTimeConverterToCurrentCulture() }; gvCol.Header = T.Texts[rpt.ReportType.ToString() + "_" + dataCol.Caption]; if (dataCol.ColumnName=="Status" && dataCol.DataType==typeof(System.Int16)) { DataTemplate cell = new DataTemplate(); Binding bind = new Binding(dataCol.ColumnName); FrameworkElementFactory factory = new FrameworkElementFactory(typeof(TextBlock)); //factory.SetBinding(TextBlock.TextProperty, bind);//the second parameter should be bind factory.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Center);//set the text align to right factory.SetValue(TextBlock.BackgroundProperty, System.Windows.Media.Brushes.Blue); factory.SetValue(TextBlock.ForegroundProperty, new SolidColorBrush(Colors.Yellow)); cell.VisualTree = factory; gvCol.CellTemplate = cell; } gvParameterList.Columns.Add(gvCol); } }

lvParameterList.DataContext = dt;

lvParameterList.DataContext = dt;

我在这里所做的基本上是,如果列名称为"Status",则其绑定值为0或1.
最后,如果值为0,则需要将背景色设置为绿色,如果将1设置为红色,但是为了简化起见,我只需在此单元格中添加一个文本框并更改一些属性以查看其是否适用.

What I am basically doing here is that if the column name is "Status", its binding value is either 0 or 1.
At the end If it is 0 I need to set the color of background as green and if 1 as red, but for simplification I simply add a textbox to this cell and chge some property to see if it applies.

但是数据模板根本不适用.

But the datatemplate is not apply at all.

 商品来源的上下文由最后一行定义

 The context for the item source is defined by the last line

有什么想法为什么数据模板不适用于状态"列单元格?

Any idea why the datatemplate is not apply to the Status column cell ?

致谢

推荐答案

在上面的代码中查看.

As you look at your code above.

您能看到多少?

如果您使人们难以接受,那么获得良好答案的机会就会降低.

If you make it hard for people then your chance of getting a good answer is lower.

如果人们怀疑您在问题上没有花太多力气,那么他们很有可能会认为回答这个问题将很艰辛.

If people suspect you've not put much effort into your question then there's a fair chance they will assume it will be hard work answering it.

只有MSFT可以付费.

Only MSFT are paid to do so.

.

我要说的第一件事是,在代码中构建UI应该是您选择的绝对最后的选择.

The first thing I would say is that building UI in code should be the absolute last option you choose.

这比xaml或将xaml构建为字符串或选择模板等要困难得多.

It's way harder to do than xaml or building xaml as strings or selecting templates etc.

发件人:

https://msdn.microsoft.com/zh-cn/library/system.windows.frameworkelementfactory(v = vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.windows.frameworkelementfactory(v=vs.110).aspx

"


这篇关于如何在C#中将DataTemplate添加到特定的GridView Column单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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