예제 #1
0
        internal MainWindowViewModel(Settings settings)
        {
            this.readOnlyEvents     = new ReadOnlyObservableCollection <Event>(this.events);
            this.consumerConnection = new ConnectionViewModel(this);
            this.providerConnection = new ConnectionViewModel(this);
            this.canEditSettings    = CalculatedProperty.Create(
                this.GetProperty(o => o.IsStarted),
                this.GetProperty(o => o.IsStopped),
                (isStarted, isStopped) => !isStarted && isStopped,
                this.GetProperty(o => o.CanEditSettings));
            #region  CalculatedProperty2
            this.canStart = CalculatedProperty.Create(
                this.GetProperty(o => o.IsStarted),
                this.GetProperty(o => o.IsStopped),
                this.GetProperty(o => o.ListeningPort),
                this.GetProperty(o => o.ProviderPort),
                this.GetProperty(o => o.LogFolder),
                (isStarted, isStopped, lp, pp, lf) => !isStarted && isStopped && string.IsNullOrEmpty(ValidatePort(lp) + ValidatePort(pp) + ValidateFolder(lf)),
                this.GetProperty(o => o.CanStart));
            #endregion
            this.canStop = CalculatedProperty.Create(
                this.GetProperty(o => o.IsStopped), s => !s, this.GetProperty(o => o.CanStop));
            this.settings = settings;

            #region TwoWayBinding
            TwoWayBinding.Create(
                this.settings.GetProperty(o => o.ListeningPort), this.GetProperty(o => o.ListeningPort));
            TwoWayBinding.Create(
                this.settings.GetProperty(o => o.ProviderHostName), this.GetProperty(o => o.ProviderHostName));
            TwoWayBinding.Create(
                this.settings.GetProperty(o => o.ProviderPort), this.GetProperty(o => o.ProviderPort));
            TwoWayBinding.Create(
                this.settings.GetProperty(o => o.LogFolder), this.GetProperty(o => o.LogFolder));
            TwoWayBinding.Create(
                this.settings.GetProperty(o => o.AutoScrollToMostRecentEvent),
                a => a,
                this.GetProperty(o => o.AutoScrollToMostRecentEvent),
                a => a.GetValueOrDefault());
            #endregion

            this.AddValidationRule(this.GetProperty(o => o.ProviderPort), ValidatePort);
            this.AddValidationRule(this.GetProperty(o => o.ListeningPort), ValidatePort);
            this.AddValidationRule(this.GetProperty(o => o.LogFolder), ValidateFolder);
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        internal ConnectionViewModel(MainWindowViewModel parent)
        {
            this.parent          = parent;
            this.connectionCount = CalculatedProperty.Create(
                this.parent.GetProperty(o => o.IsStopped),
                this.GetProperty(o => o.ConnectionCountCore),
                (s, c) => GetCount(!s, c),
                this.GetProperty(o => o.ConnectionCount));
            this.bytesReceived = CalculatedProperty.Create(
                this.GetProperty(o => o.Client),
                this.GetProperty(o => o.BytesReceivedCore),
                (c, r) => GetCount(c != null, r),
                this.GetProperty(o => o.BytesReceived));
            this.secondsSinceLastReceived = CalculatedProperty.Create(
                this.GetProperty(o => o.Client),
                this.parent.GetProperty(o => o.Now),
                this.GetProperty(o => o.LastReceived),
                (c, n, r) => c == null ? string.Empty : ((long)(n - r).TotalSeconds).ToString(CultureInfo.InvariantCulture),
                this.GetProperty(o => o.SecondsSinceLastReceived));
        }
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        CustomPropertyTypeDescriptor.Register(typeof(OrderLine),
                                              CalculatedProperty.Create("Total", (OrderLine source) => source.Quantity * source.Price)
                                              );

        var form = new Form();
        var dg   = new DataGridView {
            Dock = DockStyle.Fill, Parent = form
        };

        dg.DataSource = Enumerable.Range(1, 10).Select(n => new OrderLine
        {
            ItemNo   = "Item#" + n,
            Quantity = n,
            Price    = 10 * n
        }).ToList();

        Application.Run(form);
    }