登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

学无止境

一点积累,与大家分享

 
 
 

日志

 
 

CollectionAggregator例子  

2010-05-19 18:21:01|  分类: C#技术 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
<UserControl
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:Interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:local="clr-namespace:CollectionAggregatorDemo"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Class="CollectionAggregatorDemo.MainView"
             Height="Auto" Width="600" HorizontalContentAlignment="Center" mc:Ignorable="d">
    <Grid Background="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}">
        <Grid.RowDefinitions>
            <RowDefinition Height="20"/>
            <RowDefinition Height="210"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="400" />
            <ColumnDefinition Width="200"/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Row="0" Grid.Column="0" Text="Master" HorizontalAlignment="Center" />
        <ListView Grid.Column="0" ItemsSource="{Binding MasterCollection, Mode=Default}" SelectedItem="{Binding CurrentMaster, Mode=OneWayToSource}" IsSynchronizedWithCurrentItem="True" RenderTransformOrigin="0.5,0.5" Margin="5.923,1.064,7.144,-11.87" Background="#FFD0C89E" Grid.Row="1">
            <ListView.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="0.553"/>
                    <TranslateTransform/>
                </TransformGroup>
            </ListView.RenderTransform>
            <ListView.View>
                <GridView >
                    <GridViewColumn Width="50" DisplayMemberBinding="{Binding Id}" >
                        <GridViewColumnHeader Content="ID" Background="#FFB6A439"/>
                    </GridViewColumn>
                    <GridViewColumn Width="70" DisplayMemberBinding="{Binding Name}" >
                        <GridViewColumnHeader Content="Name" Background="#FFA9993D"/>
                    </GridViewColumn>
                    <GridViewColumn Width="100"  >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock HorizontalAlignment="Stretch">
                                    <Interactivity:Interaction.Behaviors>
                                      <local:DetailAggregatorBehavior Aggregation="Sum" Collection="{Binding Details}" TargetPropertyName="Text" />
                                    </Interactivity:Interaction.Behaviors>
                                </TextBlock>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                        <GridViewColumnHeader Content="Sum" Background="#FFB8A746"/>
                    </GridViewColumn>

                    <GridViewColumn Width="100"  >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock HorizontalAlignment="Stretch">
                                    <Interactivity:Interaction.Behaviors>
                                      <local:DetailAggregatorBehavior Aggregation="Average" Collection="{Binding Details}" TargetPropertyName="Text" />
                                    </Interactivity:Interaction.Behaviors>
                                </TextBlock>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                        <GridViewColumnHeader Content="Average" ToolTip="Average" Background="#FFAF9F46" Width="159.949"   />
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
        <TextBlock Grid.Row="0" Grid.Column="1" Text="Details" HorizontalAlignment="Center" />
        <ListView Grid.Row="1" Grid.Column="1" ItemsSource="{Binding CurrentMaster.Details}" IsSynchronizedWithCurrentItem="True">
            <ListView.View>
                <GridView >
                    <GridViewColumn Width="170">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox MinWidth="100" HorizontalAlignment="Stretch" Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                        <GridViewColumnHeader Content="Value" Background="#FF877822"/>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

        <Border Margin="10" BorderThickness="1" BorderBrush="Black" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2">
            <StackPanel >
                <TextBlock Margin="10" TextWrapping="Wrap"><Run Text="This application demonstrates the realtime Collection Aggregation options for Master-Detail relationships."/></TextBlock>
                <TextBlock Margin="10" TextWrapping="Wrap"><Run Text="The following controls are bound using an ObservableCollectionAggregator&lt;T&gt; directly in the ViewModel. The aggregator is regenerated when the SelectedItem changes."/></TextBlock>
                <Border BorderThickness="1" BorderBrush="Black">
                    <StackPanel Orientation="Horizontal" >
                        <TextBlock Text="Count: " Padding="5" FontWeight="Bold" />
                        <TextBlock Text="{Binding Aggregator.Count, Mode=OneWay}" Padding="5" FontWeight="Bold" />
                        <TextBlock Text="Sum: " Padding="5" FontWeight="Bold" />
                        <TextBlock Text="{Binding Aggregator.Sum, Mode=OneWay}" Padding="5" FontWeight="Bold" />
                        <TextBlock Text="Average: " Padding="5" FontWeight="Bold" />
                        <TextBlock Text="{Binding Aggregator.Average, Mode=OneWay}" Padding="5" FontWeight="Bold" />
                    </StackPanel>
                </Border>
                <TextBlock Margin="10" TextWrapping="Wrap"><Run Text="As you can see, changing any item in the details section will automatically keep this updated in real-time."/></TextBlock>
                <Path Fill="Black" Stretch="Fill" Stroke="Black" Margin="20,0,20,0" Height="1" Data="M22,0.28 L472,0.28"/>
                <TextBlock Margin="10" TextWrapping="Wrap"><Run Text="The GridView poses a different challenge, however."/></TextBlock>
                <TextBlock Margin="10" TextWrapping="Wrap"><Run Text="Here, you cannot bind directly to a member, since we're trying to use this within a CellTemplate in an easy fashion. In order to work around this, we use a custom behavior."/></TextBlock>
                <TextBlock Margin="10" TextWrapping="Wrap"><Run Text="The behavior is written in a way that it binds to the collection, specifies how to aggregate values, and also a target property on it's AssociatedObject on which it will set the results of the aggregation. This allows its use in places that would otherwise be challenging without code behind."/></TextBlock>
            </StackPanel>
        </Border>
    </Grid>
</UserControl>  


// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MainViewModel.cs" company="Reed Copsey, Jr.">
//   Copyright 2009, Reed Copsey, Jr.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace CollectionAggregatorDemo
{
    using System.Collections.ObjectModel;
    using System.ComponentModel;

    using CollectionAggregator;
    using CollectionAggregatorDemo.Model;

    /// <summary>
    /// The Main View Model for our demonstration project
    /// </summary>
    public class MainViewModel : INotifyPropertyChanged
    {
        /// <summary>
        /// Should we let our window close?
        /// </summary>
        private readonly ObservableCollection<Master> masterCollection;

        /// <summary>
        /// The current Master record
        /// </summary>
        private Master currentMaster;

        /// <summary>
        /// The aggregator to use for our collection
        /// </summary>
        private ObservableCollectionAggregator<Detail> aggregator;

        /// <summary>
        /// Initializes a new instance of the <see cref="MainViewModel"/> class.
        /// </summary>
        public MainViewModel()
        {
            // Create some default data...
            this.masterCollection = new ObservableCollection<Master>
                                    {
                                        new Master(0)
                                        {
                                            Name = "Zero"
                                        },
                                        new Master(1)
                                        {
                                            Name = "One"
                                        },
                                        new Master(2)
                                        {
                                            Name = "Two"
                                        },
                                        new Master(3)
                                        {
                                            Name = "Three"
                                        },
                                        new Master(4)
                                        {
                                            Name = "Four"
                                        },
                                        new Master(5)
                                        {
                                            Name = "Five"
                                        }
                                    };
        }

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Gets or sets the current master.
        /// </summary>
        /// <value>The current master.</value>
        public Master CurrentMaster
        {
            get
            {
                return this.currentMaster;
            }
           
            set
            {
                if (this.currentMaster != value)
                {
                    this.currentMaster = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("CurrentMaster"));
                    }

                    if (this.currentMaster == null)
                    {
                        this.Aggregator = null;
                    }
                    else
                    {
                        ObservableCollectionAggregator<Detail> aggr = new ObservableCollectionAggregator<Detail>(this.currentMaster.Details as ObservableCollection<Detail>, d => d.Value);
                        this.Aggregator = aggr;
                    }
                }
            }
        }

        /// <summary>
        /// Gets the aggregator.
        /// </summary>
        /// <value>The aggregator.</value>
        public ObservableCollectionAggregator<Detail> Aggregator
        {
            get
            {
                return this.aggregator;
            }

            private set
            {
                if (this.aggregator != value)
                {
                    this.aggregator = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Aggregator"));
                        //this.PropertyChanged(this, new PropertyChangedEventArgs("Sum"));

                    }
                }
            }
        }

        /// <summary>
        /// Gets the master collection.
        /// </summary>
        /// <value>The master collection.</value>
        public ObservableCollection<Master> MasterCollection
        {
            get
            {
                return this.masterCollection;
            }
        }
    }
}
  评论这张
 
阅读(619)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018