Using the WPF DatePicker Toolkit as a Parameter for an ObjectDataProvider Used as a Data Source

This man beat me;

I have a WPF window with two (important for this case) controls, as from the WPF toolkit available in CodePlex; DatePicker and DataGrid.

A CLR object is installed in the DataContext of this window, which has all the information it needs. This CLR object has a large list of data and a method called GetDataForDate (DateTime date), which determines what date we will see the data.

How can I create an ObjectDataProvider (which, I believe, will be the right solution) to which a datagrid can be attached, which provides access to the data returned by GetDataForDate (), called with the selected DatePicker as a parameter?

In other words, I want the user to use the datepicker to select a date, and the grid should automatically update whenever the date changes to display the correct data.

What black magic should I do to achieve something similar, which I think should be a relatively common data binding scenario?

Thanks in advance!

+3
source share
4 answers

Here is my complete code. Hope this helps.

Xaml code:

<Window x:Class="DataGridSort.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    xmlns:System="clr-namespace:System;assembly=mscorlib"
    Title="Window1" Height="413" Width="727"
        x:Name="_this">
    <Window.Resources>
        <ObjectDataProvider ObjectInstance="_this.DataContext"
                            MethodName="GetFromDate"
                            x:Key="odp">
            <ObjectDataProvider.MethodParameters>
                <System:DateTime>2008-01-01</System:DateTime>  
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <dg:DatePicker Grid.Row="0" x:Name="dtpSource" >
            <dg:DatePicker.SelectedDate>
                <Binding Source="{StaticResource odp}"
                         Path="MethodParameters[0]"   
                             BindsDirectlyToSource="True" 
                             Mode="OneWayToSource"/>
            </dg:DatePicker.SelectedDate>
        </dg:DatePicker>

        <dg:DataGrid x:Name="dtgGrid"
                          ItemsSource="{Binding Source={StaticResource odp}}"
                          AutoGenerateColumns="True"
                          Grid.Row="1"/>
    </Grid>
</Window>

Code behind:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        LoadData();
    }

    protected void LoadData()
    {
        DataContext = new Data();
        ObjectDataProvider odp = this.Resources["odp"] as ObjectDataProvider;

        odp.ObjectInstance = DataContext;
    }
}

and business object:

public class DataItem
{
    public string Name { get; set; }
    public int BirthYear { get; set; }
}

public class Data
{
    private readonly List<DataItem> data;

    public Data()
    {
        data = new List<DataItem>();
        data.Add(new DataItem { Name = "John", BirthYear = 2007 });
        data.Add(new DataItem { Name = "Mike", BirthYear = 2007 });
        data.Add(new DataItem { Name = "Aaron", BirthYear = 2006 });
        data.Add(new DataItem { Name = "Bill", BirthYear = 2006 });
        data.Add(new DataItem { Name = "Steven", BirthYear = 2005 });
        data.Add(new DataItem { Name = "George", BirthYear = 2004 });
        data.Add(new DataItem { Name = "Britany", BirthYear = 2004 });
    }

    public List<DataItem> GetFromDate(DateTime dt)
    {
        return this.data.Where(d => d.BirthYear == dt.Year).ToList();
    }
}
+6
source

OneWayToSource DatePicker.SelectedDate, MethodParameter ObjectDataProvider.

ObjectDataProvider:

   <ObjectDataProvider ObjectType="{x:Type theObjectType}" 
                        MethodName="GetDataForDate"
                        x:Key="odp">
        <ObjectDataProvider.MethodParameters>
            <System:DateTime>2008-01-01</System:DateTime>  
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>

SelectedDate DatePicker ObjectDataProvider:

<dg:DatePicker x:Name="datePicker" >
    <dg:DatePicker.SelectedDate>
        <Binding Source="{StaticResource odp}"
                 Path="MethodParameters[0]"   
                     BindsDirectlyToSource="True" 
                     Mode="OneWayToSource"/>
    </dg:DatePicker.SelectedDate>
</dg:DatePicker>

, , ObjectDataProvider ObjectDataProvider:

<dg:DataGrid x:Name="dtgGrid"
             ItemsSource="{Binding Source={StaticResource odp}}"
             AutoGenerateColumns="False"/>
+1

, SelectedDate DatePicker DateTime ( : DateTime, DateTime). , , , DateTime, WPF , Rune Jacobsen, .

! BTW

+1

INotifyPropertyChanged :

public class MyDataObject : INotifyPropertyChanged
{
    private DateTime _SelectedDate;
    public DateTime SelectedDate
    {
        get
        {
            return _SelectedDate;
        }
        set
        {
            _SelectedDate = value;
            NotifyPropertyChanged("SelectedDate");
            GetDataForDate();
        }
    }

    private ObservableCollection<YourDataType> _Data;
    public ObservableCollection<YourDataType> Data
    {
        get
        {
            return _Data;
        }
        set
        {
            _Data = value;
            NotifyPropertyChanged("Data");
        }
    }

    public void GetDataForDate()
    {
        // Your code here to fill the Data object with your data
    }


    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

ObjectDataProvider XAML . :

<ObjectDataProvider x:Key="MyDataSource" ObjectType="{x:Type local:MyDataObject}" />

:

<DockPanel>
    <toolkit:DatePicker SelectedDate="{Binding Path=SelectedDate, Mode=Default, Source={StaticResource MyDataSource}}"/>
    <toolkit:DataGrid ItemsSource="{Binding Path=Data, Mode=Default, Source={StaticResource MyDataSource}}"/>
</DockPanel>
0
source

Source: https://habr.com/ru/post/1699683/


All Articles