在行单元格中使用按钮和组合框 [英] Using Button and Combo Box in the Row Cells
问题描述
我正在使用WPF来构建数据网格。但是我的datagrid表需要横向数据。
所以,我已经用这种方式准备数据,将行表示为列,将列表示为行。
但是现在我需要一个带按钮的行和一个带有组合框的行。可能吗? (如下表结构)
Rows | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
A | 数据 | 数据 | 数据 | 数据 | 数据 | 数据 | d ata |
B | 数据 | 数据 | 数据 | 数据 | 数据 | 数据 | 数据 |
C | 按钮 | 按钮 | 按钮 | 按钮 | 按钮 | 按钮 | |
D | Combo | 组合 | 组合 | 组合 | 组合 | 组合 | 组合 |
如果有这样的方式,请告诉我。
谢谢提前!!
Ram
嗨k .ramc,
通常,一个集合对应一个 datagrid,但是你想要两个类型集合对应一个数据网格。
如果你真的想这样做,我建议你可以将两个数据网格组合成一个数据网格,用于一个Usercontrol。
< DataGrid
AutoGenerateColumns =" False"
CanUserAddRows =" False"
ItemsSource =" {Binding infos}"
RowHeaderWidth =" 0">
< DataGrid.Columns>
< DataGridTextColumn
Width =" 150"
Binding =" {Binding field1}"
Header =" field1" />
< DataGridTextColumn
Width =" 150"
Binding =" {Binding field2}"
Header =" field2" />
< DataGridTextColumn
Width =" 150"
Binding =" {Binding field3}"
Header =" field3" />
< /DataGrid.Columns>
< / DataGrid>
< DataGrid
AutoGenerateColumns =" False"
HeadersVisibility =" None"
ItemsSource =" {Binding info1s}">
< DataGrid.Columns>
< DataGridTemplateColumn Width =" 150"头= QUOT; field1的">
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< Grid>
< ContentPresenter Height =" 18" Content =" {Binding field1}" />
< / Grid>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>
< DataGridTemplateColumn Width =" 150"头= QUOT; FIELD2">
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< Grid>
< ContentControl Height =" 18" Content =" {Binding field2}" />
< / Grid>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>
< DataGridTemplateColumn Width =" 150"头= QUOT;字段3">
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< Grid>
< ContentControl Height =" 18" Content =" {Binding field3}" />
< / Grid>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>
< /DataGrid.Columns>
< / DataGrid>
public partial class Window24:Window
{
public ObservableCollection< info>信息{get;组; }
public ObservableCollection< info1> info1s {get;组; }
public Window24()
{
InitializeComponent();
infos = new ObservableCollection< info>()
{
new info(){field1 =" test",field2 =" test",field3 =" test" ;},
新信息(){field1 = 1,field2 = 2,field3 = 3}
};
info1s = new ObservableCollection< info1>()
{
new info1(){field1 = new Button(),field2 = new Button(),field3 = new Button( ),
new info1(){field1 = new ComboBox(),field2 = new ComboBox(),field3 = new ComboBox()}
};
this.DataContext = this;
}
}
公共类信息
{
公共对象field1 {get;组; }
公共对象field2 {get;组; }
公共对象field3 {get;组; }
}
公共类信息1
{
public UIElement field1 {get;组; }
public UIElement field2 {get;组; }
public UIElement field3 {get;组; }
}
最好的问候,
Cherry
Hi,
I'm using WPF for building a datagrid. But my datagrid table needed data to be across horizontally.
So, I have prepared data in such a way to represent rows as columns and columns as rows.
But now I need a row with buttons and one with Combo box. Is it possible? (like below table structure)
Rows | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
A | data | data | data | data | data | data | data |
B | data | data | data | data | data | data | data |
C | Button | Button | Button | Button | Button | Button | Button |
D | Combo | Combo | Combo | Combo | Combo | Combo | Combo |
Please let me know if there is such a way.
Thanks in Advance!!
Ram
Hi k.ramc,
Usually, one collection corresponds to one datagrid, but you want two types collection corresponds to one datagrid.
If you really want to do this, I suggest you can combine two datagrid to one datagrid, for one Usercontrol.
<DataGrid AutoGenerateColumns="False" CanUserAddRows="False" ItemsSource="{Binding infos}" RowHeaderWidth="0"> <DataGrid.Columns> <DataGridTextColumn Width="150" Binding="{Binding field1}" Header="field1" /> <DataGridTextColumn Width="150" Binding="{Binding field2}" Header="field2" /> <DataGridTextColumn Width="150" Binding="{Binding field3}" Header="field3" /> </DataGrid.Columns> </DataGrid> <DataGrid AutoGenerateColumns="False" HeadersVisibility="None" ItemsSource="{Binding info1s}"> <DataGrid.Columns> <DataGridTemplateColumn Width="150" Header="field1"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Grid> <ContentPresenter Height="18" Content="{Binding field1}" /> </Grid> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTemplateColumn Width="150" Header="field2"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Grid> <ContentControl Height="18" Content="{Binding field2}" /> </Grid> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTemplateColumn Width="150" Header="field3"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Grid> <ContentControl Height="18" Content="{Binding field3}" /> </Grid> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>public partial class Window24 : Window { public ObservableCollection<info> infos { get; set; } public ObservableCollection<info1> info1s { get; set; } public Window24() { InitializeComponent(); infos = new ObservableCollection<info>() { new info(){field1="test",field2="test",field3="test"}, new info(){field1=1,field2=2,field3=3} }; info1s = new ObservableCollection<info1>() { new info1(){field1=new Button(),field2=new Button(),field3=new Button()}, new info1(){field1=new ComboBox(),field2=new ComboBox(),field3=new ComboBox()} }; this.DataContext = this; } } public class info { public object field1 { get; set; } public object field2 { get; set; } public object field3 { get; set; } } public class info1 { public UIElement field1 { get; set; } public UIElement field2 { get; set; } public UIElement field3 { get; set; } }
Best Regards,
Cherry
这篇关于在行单元格中使用按钮和组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!