可观察的继承 collection
inheritance of observable collection
我需要继承我的 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;
}
}
如您所见,我可以在构造函数中对其进行静态初始化。
然而,我实际上是在运行时获取这个 collection。如何将 collection 分配给此 class?
即时获取的 collection 是 ObservableCollection<UsageStatisctics>
。
我需要封装的原因是因为基础设施需要这个:
<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>
请建议我如何将 collection 添加到 UsageCollection
class,或者如何使用它的替代方法。
UsageCollection
没有封装ObservableCollection<UsageStatistics>
,是继承。封装...
public class UsageCollection {
protected ObservableCollection<UsageStatistics> theCollection;
}
现在它被包含了,我们可以通过构造函数注入一个集合对象。
public UsageCollection ( ObservableCollection<UsageStatistics> someCollection );
How can I assign that collection to this class?
要访问存储在 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"));
顺便说一句:您可以将样本数据嵌入 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>
以下示例演示了如何将您的集合与数据网格结合使用。如果它能解决您的问题,请告诉我。
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;
}
}
您的数据模板:
<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>
我需要继承我的 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;
}
}
如您所见,我可以在构造函数中对其进行静态初始化。
然而,我实际上是在运行时获取这个 collection。如何将 collection 分配给此 class?
即时获取的 collection 是 ObservableCollection<UsageStatisctics>
。
我需要封装的原因是因为基础设施需要这个:
<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>
请建议我如何将 collection 添加到 UsageCollection
class,或者如何使用它的替代方法。
UsageCollection
没有封装ObservableCollection<UsageStatistics>
,是继承。封装...
public class UsageCollection {
protected ObservableCollection<UsageStatistics> theCollection;
}
现在它被包含了,我们可以通过构造函数注入一个集合对象。
public UsageCollection ( ObservableCollection<UsageStatistics> someCollection );
How can I assign that collection to this class?
要访问存储在 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"));
顺便说一句:您可以将样本数据嵌入 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>
以下示例演示了如何将您的集合与数据网格结合使用。如果它能解决您的问题,请告诉我。
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;
}
}
您的数据模板:
<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>