使用命令(mvvm)将数据从视图发送到viewmodel

时间:2015-09-03 20:12:18

标签: c# wpf mvvm data-binding

如何将数据从视图发送到视图模型?因此,当我单击一个按钮时,它会将数据(startDate,endDate,product和category)发送到视图模型。在视图模型中,我将使用4个值作为方法调用的参数。

以下是View中的代码:

    <DatePicker SelectedDate="{Binding startDate}"
        ......>

    <DatePicker SelectedDate="{Binding endDate}"
        ......>

    <ComboBox SelectedItem="{Binding productName}"
        ......>
        <ComboBoxItem Content="X"/>
        <ComboBoxItem Content="Y"/>
        <ComboBoxItem Content="Z"/>
    </ComboBox>

    <ComboBox SelectedItem="{Binding category}"
        ......>
        <ComboBoxItem Content="CD/Web"/>
        <ComboBoxItem Content="Operating System"/>
        <ComboBoxItem Content="OSConfiguration"/>
        <ComboBoxItem Content="..."/>
        <ComboBoxItem Content="..."/>
        <ComboBoxItem Content="..."/>
    </ComboBox>

    <Button Command="{Binding AddMatricCommand}" Content="Query" 
    ....../>

以下是View Model中的代码,我需要startDate,endDate,product和category作为参数来调用View Model中的方法。

{
    [Export(typeof(IShell))]
    public class ShellViewModel : Screen
    {
        #region Members
    private AnalyticsManager _manager;
    private DataTable _table;

    #endregion

    #region Properties

    /// <summary>
    /// Public property that is bound to the DataGrid in the view
    /// </summary>
    public DataTable Table
    {
        get
        {
            return _table;
        }
        set
        {
            _table = value;
            NotifyOfPropertyChange(() => Table);
        }
    }

    private string _startDate;
    public string startDate
    {
        get { return _startDate;  }
        set
        {
            _startDate = value;
            NotifyOfPropertyChange(startDate);
        }
    }

    private string _endDate;
    public string endDate
    {
        get { return _endDate; }
        set
        {
            _startDate = value;
            NotifyOfPropertyChange(endDate);
        }
    }

    private string _productName;
    public string productName
    {
        get { return _productName; }
        set
        {
            _startDate = value;
            NotifyOfPropertyChange(productName);
        }
    }

    private string _category;
    public string category
    {
        get { return _category; }
        set
        {
            _startDate = value;
            NotifyOfPropertyChange(category);
        }
    }

    #endregion

    /// <summary>
    /// Constructor for the view model
    /// </summary>
    public ShellViewModel()
    {
        //Table = new DataTable();
        Init();
    }

    /// <summary>
    /// Sets up Analytics service and makes a request
    /// </summary>
    private void Init()
    {
        // start mananger
        // required component for authentication
        // todo: a more secure way of storing and reading the client secrets
        var clientSecret = Directory.GetCurrentDirectory() + @"\client_secrets.json";
        _manager = new AnalyticsManager(clientSecret);

        // set the static ID for the project we're pulling data from
        _manager.LoadAnalyticsProfiles();
        _manager.SetDefaultAnalyticProfile("12345678");

        // todo: define metrics, dimensions, and filters using the UI
        // define "x value" metrics

        var metrics = new List<DataItem>();
        metrics.Add(EventTracking.Metrics.uniqueEvents);
        //metrics.Add(Session.Metrics.visits);

        // define "y value" dimensions
        var dimensions = new List<DataItem>();
        dimensions.Add(EventTracking.Dimensions.eventCategory);
        dimensions.Add(GeoNetwork.Dimensions.country);
        //dimensions.Add(GeoNetwork.Dimensions.city);

        // filter by something to reduce the size of the request
        var filters = new List<DataItem>();

        var country = new DataItem(GeoNetwork.Dimensions.country.Name);
        country.Equals("United States");
        filters.Add(country);

        var eventCategory = new DataItem(EventTracking.Dimensions.eventCategory.Name);
        //eventCategory.Contains("277");
        //filters.Add(eventCategory);

        // get table from Google and set to view property
        Table = _manager.GetGaDataTable(DateTime.Today.AddDays(-30), DateTime.Today, metrics, dimensions, filters, metrics, true);
    }
}
}

0 个答案:

没有答案