手动创build未绑定的数据网格

手动创build未绑定的数据网格,第1张

概述手动创build未绑定数据网格

任何人都可以告诉我是否可以创build和添加数据到未绑定的WPF Toolkit数据网格。

如果事实上可以做到的话,有人可以举一个例子:

以编程方式:创build一个datagrID创build一个datagrIDtextcolumn添加datagrIDtextcolumn到datagrID创build一个datagrIDrow设置datagrIDtextcolumn的文本行为“testing”添加datagrIDrow到数据网格

我想创build一个未绑定的数据网格。 我的理由是,我想创build一个具有不同types和数字的多个控件的模板列 – 2个checkBox,4个单选button,5个checkBox等 – 它们是在运行时dynamic添加的,因为types和数量是未知的,一个数据绑定的方法。 我很高兴自由地工作。

如何保存表单的状态?

CultureInfo在windows版本之间不一致

在列表中find一个项目的特定关键字

当我从.NET产生一个新的线程时究竟发生了什么?

在windows启动时启动wpf应用程序

先谢谢你!

[编辑:我还没有find这个问题的适当答案,并开始了赏金]

Mono和Microsoft的ASP.NET实现有什么区别?

如何在linux中获得.Net文件的AssemblyVersion

ManagedThreadID和 *** 作系统ThreadID之间的关系

免费的windows安装程序

如何访问windows中的特殊目录?

您可以使用UserControl来创建所需的控件。

Window1.xaml(片段)

<dg:DataGrID ItemsSource="{Binding}" CanUserAddRows="False" autoGenerateColumns="False"> <dg:DataGrID.Columns> <dg:DataGrIDTemplateColumn header="Test" MinWIDth="100"> <dg:DataGrIDTemplateColumn.CellTemplate> <DataTemplate> <my:ControlA Foo="{Binding}"></my:ControlA> </DataTemplate> </dg:DataGrIDTemplateColumn.CellTemplate> </dg:DataGrIDTemplateColumn> </dg:DataGrID.Columns> </dg:DataGrID>

Window1.xaml.cs

public partial class Window1 : Window { List<Foo> _items = new List<Foo>(); public Window1() { InitializeComponent(); _items.Add(new Foo { CheckBoxCount = 2,TextBoxCount = 1 }); _items.Add(new Foo { CheckBoxCount = 3,TextBoxCount = 0 }); DataContext = _items; } }

ControlA.xaml

<UserControl x:Class="Demo.ControlA" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <StackPanel x:name="_placeHolder"> </StackPanel> </UserControl>

ControlA.xaml.cs

public partial class ControlA : UserControl { public ControlA() { InitializeComponent(); Loaded += new RoutedEventHandler(ControlA_Loaded); } voID ControlA_Loaded(object sender,RoutedEventArgs e) { if (Foo != null) { for (int it = 0; it < Foo.CheckBoxCount; it++) _placeHolder.Children.Add(new CheckBox()); for (int it = 0; it < Foo.TextBoxCount; it++) _placeHolder.Children.Add(new TextBox()); } } public static Readonly DependencyProperty FooProperty = DependencyProperty.Register("Foo",typeof(Foo),typeof(ControlA)); public Foo Foo { get { return (Foo)GetValue(FooProperty); } set { SetValue(FooProperty,value); } } }

Foo.cs

public class Foo { public int TextBoxCount { get; set; } public int CheckBoxCount { get; set; } }

希望这可以帮助。

PS应该可以将DataGrIDTemplateColumn 重构为单独的UserControl。

我从来没有使用一个网格,没有绑定到某些东西。

但是看看这个潜在的原因,可以在使用数据绑定的时候解决。

例如:如果您使用viewmodel类来反对网格绑定,那么您可以使该类的一个方面成为各种控件的可见性设置器。

在对象中,你会有这样的:

Public Readonly Property CheckBoxAVisibility As windows.Visibility Get ' Do Logic Return Visiblity End Get End Property

在XAML中你可以这样做:

<CheckBox IsChecked="{Binding IsBoxAChecked}" Visibility={Binding CheckBoxAVisibility}" />

这也使事情变得更容易,因为您可以通过修改行内的其他控件来修改各种控件的可见性(例如,取消选中CheckBoxA会导致Radiobutton B,C和D出现)。

第二种选择是在网格中仅列出“标题”信息,当用户双击一行时,将在辅助面板或窗口中显示具有可编辑方面的编辑器(类似于MVC编辑的工作方式)。

编辑由于评论

这是另一个建议,而不是一个数据网格,使用StackPanel。 对于您需要的每一行,您可以添加一个网格面板或堆栈面板或类似的东西,根据您的规则在运行时创建。 例如:

XAML:

<StackPanel name="stkItems" OrIEntation="Vertical" />

码:

Public Sub AddItems(dt as Datatable) For Each row As DaTarow in dt.Rows Select Case row("Which") Case 1: Dim i As New CheckBox i.Content = "Foo" i.IsChecked = row("Content") stkItems.Children.Add(i) Case 2: Dim i as New TextBox i.Text = row("Content") stkItems.Children.Add(i) End Select Next End Sub

这是非常简单的,你可以做一些事情,比如你可以使用预定义的用户控件,或者只是将一堆控件构建到编程定义的容器中,然后将其添加到StackPanel

我创建了一个从DataGrIDBoundColumn派生的简单的DataGrIDUnboundedColumn类,可以用来为单元格提供FrameworkElement的自定义文本。

您只需订阅CellFormating事件,并将EventArgs中的CellElement设置为将显示的自定义元素。 也可以在EventArgs中设置CellText,在这种情况下,具有CellText的TextBlock将显示在网格中。

以下示例显示如何使用它:

XAML:

<dg:DataGrID name="DataGrID1" autoGenerateColumns="False"> <dg:DataGrID.Columns> <dg:DataGrIDTextColumn header="name" Binding="{Binding Path=name}"/> <myColumn:DataGrIDUnboundedColumn x:name="AddressColumn" header="Address" /> </dg:DataGrID.Columns> </dg:DataGrID>

码:

public MyPage() { InitializeComponent(); AddressColumn.CellFormating += new UnboundedColumnEventHandler(AddressColumn_CellFormating); } voID AddressColumn_CellFormating(object sender,UnboundedColumnEventArgs e) { IPerson person; person= e.DataItem as IPerson; if (person!= null) e.CellText = string.Format("{0},{1} {2}",person.Address,person.PostalCode,person.City); }

DataGrIDUnboundedColumn实现在这里:

class DataGrIDUnboundedColumn : DataGrIDBoundColumn { public event UnboundedColumnEventHandler CellFormating; public DataGrIDUnboundedColumn() { this.IsReadonly = true; } protected overrIDe FrameworkElement GenerateEditingElement(DataGrIDCell cell,object dataItem) { return null; } protected overrIDe FrameworkElement GenerateElement(DataGrIDCell cell,object dataItem) { FrameworkElement shownElement; UnboundedColumnEventArgs eventArgs; if (CellFormating == null) return null; eventArgs = new UnboundedColumnEventArgs(cell,dataItem); // call the event CellFormating(this,eventArgs); shownElement = null; // check the data set in the eventArgs if (eventArgs.CellElement != null) { // show the set eventArgs.CellElement shownElement = eventArgs.CellElement; } else if (eventArgs.CellText != null) { // show the CellText in TextBlock TextBlock textBlock = new TextBlock(); textBlock.Text = eventArgs.CellText; shownElement = textBlock; } else { // nothing set } return shownElement; } } public delegate voID UnboundedColumnEventHandler(object sender,UnboundedColumnEventArgs e); public class UnboundedColumnEventArgs : EventArgs { public DataGrIDCell Cell { get; set; } public object DataItem { get; set; } /// <summary> /// The subscriber of the event can set the CellText. /// In this case the TextBlock is used to display the text. /// NOTE that if CellElement is not null,the CellText will not be used but insted a CellElement will be shown /// </summary> public string CellText { get; set; } /// <summary> /// The subscribed can set the FrameworkElement that will be shown for this cell. /// If the CellElement is null,the CellText will be used to show the TextBlock /// </summary> public FrameworkElement CellElement { get; set; } public UnboundedColumnEventArgs() : base() { } public UnboundedColumnEventArgs(DataGrIDCell cell,object dataItem) : base() { Cell = cell; DataItem = dataItem; } }

总结

以上是内存溢出为你收集整理的手动创build未绑定的数据网格全部内容,希望文章能够帮你解决手动创build未绑定的数据网格所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1285166.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-09
下一篇2022-06-09

发表评论

登录后才能评论

评论列表(0条)

    保存