[英]WPF DataGrid Dymanic Column binding
I am new to WPF and got one problem. 我是WPF的新手,但遇到了一个问题。 I am using DataGrid
where row are fix (only 3 row) but column can be added at run time. 我正在使用DataGrid
,其中行是固定的(仅3行),但可以在运行时添加列。
Row 1 :- Stock Name
Row 2 :- Current Price
Row 3 :- Old Price
and the Row 2 and 3 will get updated at runtime
every second. 并且第2行和第3行将在runtime
每秒更新一次。 i have class as below :- 我的课如下:
class Stock
{
String name;
double currentPrice;
double oldPrice;
}
class StockCollaction
{
List<Stock> list = new List<Stock>();
public void addStock(Stock stock)
{
list.add(stock);
}
public void update()
{
....
}
.....
}
I want to create DataGrid
like below (Where each column need to bind with model, instead of row):- 我想创建如下所示的DataGrid
(其中每一列都需要与模型绑定,而不是与行绑定):
please guide me how can i get it done, any specific link to refer will be a great help, i think i have to use MVVM
. 请指导我如何完成它,任何要引用的特定链接都将提供很大的帮助,我认为我必须使用MVVM
。
In order to update the stock prices and add new stocks at runtime, Stock
should implement INotifyPropertyChanged and use ObservableCollection instead of List<Stock>
. 为了更新股票价格并在运行时添加新股票, Stock
应该实现INotifyPropertyChanged并使用ObservableCollection而不是List<Stock>
。
Expose the stock list via a public property , from the post you can also learn how to set the DataContext
and ItemsSource
of the DataGrid. 通过公共属性公开库存清单,从该帖子中,您还可以学习如何设置DataGrid的DataContext
和ItemsSource
。 This is how StockCollection class
looks like 这就是StockCollection class
样子
public class StockCollection
{
private ObservableCollection<Stock> stocks;
public ObservableCollection<Stock> Stocks
{
get
{
return stocks;
}
}
//...add(), update() and other methods/properties
}
Now the XAML code. 现在是XAML代码。
Using the built-in DataGrid
you add a new row, not a new column for a stock. 使用内置的DataGrid
您可以添加新行,而不是股票的新列。 You could find a 3rd party DataGrid
that supports inverted axes, as Mike suggested in his comment, or - this is a funny part in learning WPF - you rotate the DataGrid by applying a RotateTransform
. 正如Mike在其评论中建议的那样,您可以找到支持倒置轴的第三方DataGrid
,或者-这是学习WPF的有趣部分-您可以通过应用RotateTransform
旋转DataGrid。
In my code sample I define 2 DataGrid, one is normal, one is rotated 90 degrees. 在我的代码示例中,我定义了2个DataGrid,一个是正常的,一个是旋转90度的。 The code is modified from another post . 该代码是从另一篇文章中修改的 。 You need to play with DataGrid.ColumnHeaderStyle
, DataGrid.LayoutTransform
and DataGrid.CellStyle
to rotate the DataGrid. 您需要使用DataGrid.ColumnHeaderStyle
, DataGrid.LayoutTransform
和DataGrid.CellStyle
来旋转DataGrid。
<StackPanel Margin="100">
<DataGrid x:Name="dataGrid1" Width="200" Height="120" AutoGenerateColumns="False"
ItemsSource="{Binding Stocks}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden">
<DataGrid.Columns>
<DataGridTextColumn Header="Old Price" Binding="{Binding Path=OldPrice}"/>
<DataGridTextColumn Header="Current Price" Binding="{Binding Path=CurrentPrice}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
<Grid Height="100"></Grid>
<DataGrid x:Name="dataGrid2" Width="100" Height="500" AutoGenerateColumns="False"
ItemsSource="{Binding Stocks}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="LayoutTransform">
<Setter.Value>
<TransformGroup>
<RotateTransform Angle="90"/>
</TransformGroup>
</Setter.Value>
</Setter>
<Setter Property="Width" Value="120"/>
<Setter Property="Height" Value="30"/>
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.LayoutTransform>
<TransformGroup>
<RotateTransform Angle="-90"/>
</TransformGroup>
</DataGrid.LayoutTransform>
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="LayoutTransform">
<Setter.Value>
<TransformGroup>
<RotateTransform Angle="90"/>
</TransformGroup>
</Setter.Value>
</Setter>
<Setter Property="Width" Value="120"/>
<Setter Property="Height" Value="30"/>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Old Price" Binding="{Binding OldPrice}" />
<DataGridTextColumn Header="Current Price" Binding="{Binding CurrentPrice}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
</DataGrid.Columns>
</DataGrid>
</StackPanel>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.