我需要将Collection继承为Class,如下所示:
public class UsageCollection
: ObservableCollection<UsageStatistics>
{
public UsageCollection()
{
// static initialization
Add(new UsageStatistics("A", 6, "1/2/2012"));
Add(new UsageStatistics("C", 8, "1/2/2012"));
}
}
public class UsageStatistics
{
public string Alias { get; set; }
public int ActivityCount { get; set; }
public string Date { get; set; }
public UsageStatistics(
string alias,
int activityCount,
string date
)
{
Alias = alias;
ActivityCount = activityCount;
Date = date;
}
}
如您所见,我可以在构造函数中静态初始化它。
但是,我现在正在运行时获取此集合。如何将该集合分配给此类?
我的提取集合是ObservableCollection<UsageStatisctics>
。
我需要封装的原因是因为infragistics需要这个:
<Window.Resources>
<comboSourceCase:UsageCollection x:Key="data" />
<ig:GroupBy
x:Key="grouped"
ItemsSource="{StaticResource data}"
GroupMemberPath="Date"
KeyMemberPath="Alias"
ValueMemberPath="ActivityCount" />
</Window.Resources>
<Grid Margin="0,0,2,-2">
<ig:XamDataChart x:Name="theChart" Height="200">
<ig:XamDataChart.Axes>
<ig:CategoryXAxis x:Name="xAxis"
ItemsSource="{StaticResource grouped}"
Label="{}{Key}"/>
<ig:NumericYAxis x:Name="yAxis" />
</ig:XamDataChart.Axes>
<ig:XamDataChart.Series>
<ig:StackedColumnSeries x:Name="stack"
ItemsSource="{StaticResource grouped}"
XAxis="{Binding ElementName=xAxis}"
YAxis="{Binding ElementName=yAxis}"
AutoGenerateSeries="True"
>
</ig:StackedColumnSeries>
</ig:XamDataChart.Series>
</ig:XamDataChart>
请建议我如何添加我收到的UsageCollection
课程,或者如何使用它。
答案 0 :(得分:3)
如何将该集合分配给此类?
要访问存储在XAML资源中的集合,请使用FindResource()
var data = (UsageCollection)this.FindResource("data");
data.Clear();
data.Add(new UsageStatistics("A", 6, "1/2/2012"));
data.Add(new UsageStatistics("C", 8, "1/2/2012"));
BTW:您可以将示例数据嵌入XAML中,而不是使用构造函数:
<Window.Resources>
<comboSourceCase:UsageCollection x:Key="data" >
<comboSourceCase:UsageStatistics Alias="A" ActivityCount="3" Date= "1/2/2012"></comboSourceCase:UsageStatistics
<comboSourceCase:UsageStatistics Alias="C" ActivityCount="8" Date= "1/2/2012"></comboSourceCase:UsageStatistics
</comboSourceCase:UsageCollection>
答案 1 :(得分:0)
UsageCollection
没有封装ObservableCollection<UsageStatistics>
,它是继承的。封装......
public class UsageCollection {
protected ObservableCollection<UsageStatistics> theCollection;
}
现在它已被包含,我们可以通过构造函数注入一个集合对象。
public UsageCollection ( ObservableCollection<UsageStatistics> someCollection );
答案 2 :(得分:0)
以下示例演示了使用数据网格对集合的使用。如果它能解决您的问题,请告诉我。
public partial class Win32533638 : Window
{
public Win32533638()
{
InitializeComponent();
DgrdUsageStatistics.ItemsSource = _getUsageStatisticsFromServer();
}
private UsageCollection _getUsageStatisticsFromServer()
{
UsageCollection items = new UsageCollection();
items.Add(new UsageStatistics("D", 8, "1/2/2015"));
items.Add(new UsageStatistics("A", 6, "1/2/2012"));
return items;
}
}
您的DataTemplate:
<Window.Resources>
<DataTemplate x:Key="DataTemplate1">
<Grid d:DesignWidth="295.187" d:DesignHeight="128.271" Height="76" Width="300">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="52*"/>
<ColumnDefinition Width="57*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="44*"/>
<RowDefinition Height="45*"/>
<RowDefinition Height="52*"/>
</Grid.RowDefinitions>
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Alias" VerticalAlignment="Top" Width="119" Margin="5,5,0,5"/>
<TextBlock HorizontalAlignment="Left" Grid.Row="1" TextWrapping="Wrap" Text="Date" VerticalAlignment="Top" Width="119" Margin="5,5,0,5"/>
<TextBlock HorizontalAlignment="Left" Grid.Row="2" TextWrapping="Wrap" Text="Activity Count" VerticalAlignment="Top" Width="119" Margin="5,5,0,5"/>
<TextBlock Grid.Column="1" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Alias}" VerticalAlignment="Top" Margin="5,5,0,5"/>
<TextBlock Grid.Column="1" HorizontalAlignment="Left" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Date}" VerticalAlignment="Top" Margin="5,5,0,5"/>
<TextBlock Grid.Column="1" HorizontalAlignment="Left" Grid.Row="2" TextWrapping="Wrap" Text="{Binding ActivityCount}" VerticalAlignment="Top" Margin="5,5,0,5"/>
</Grid>
</DataTemplate>
</Window.Resources>
您的XAML代码:
<DataGrid x:Name="DrgUsageWithTemplate" Margin="0 10 0 0" ItemsSource="{StaticResource UsgCollection}" AutoGenerateColumns="False" ItemTemplate="{DynamicResource DataTemplate1}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Usage Statistical Data" CellTemplate="{StaticResource ResourceKey=DataTemplate1}">
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>