Click here to Skip to main content
15,884,648 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi All,

I am converting my WPF application over MVVM architecture. There is one xaml in my application which uses Property Grid and display data using it. Now for MVVM architecture i have to do all this through binding. I have gone through around 100+ articles but didn't get any appropriate example which shows this or which uses Property Grid in MVVM architecture.

Please urgently help me regarding this. I need a short demo which usse MVVM architecture and bind data to Property Grid.
Posted

Hi Shashank,

Maybe you don't need MVVM. You should use MVVM only when you are writing a Business-Logic + UI and you want to separate them.

If you are writing a control with UI-Logic you don't need to use MVVM!

For example, lets say I've created a "Property Grid" control that reflects some object by his properties. The "Property Grid" control expose Dependency Property for this object (lets call it "ObjectSource")so we could bind to this property later on the XAML.


If the object is Business-Logic object then you create a User-Control (The View) that contains the "Property Grid" control. And you simply and easily bind the View-Model or one of his properties to the "Property Grid" control "ObjectSource" (Dependency Property).

Try that, and if you have any more questions write the code here and I'll guid you throw it.

Best Regards,

Shai
 
Share this answer
 
Comments
Shashank Mishra 28-Apr-14 8:25am    
Hi Shai,

Thanks for your response.
Yes, in my application i need to seperate Business-Logic + UI. That's why i am following MVVM architecture.
Could you please share a sample code in which property grid data binding is done using view model properties.
Hi Shashank,

Thank you for your comment :)

I've took the liberty to write a simple style MVVM View-First using Extended WPF Toolkit PropertyGrid:

http://wpftoolkit.codeplex.com/wikipage?title=PropertyGrid[^]

The MainWindow.cs:

XML
<Window xmlns:my="clr-namespace:WpfPropertyGrid"  x:Class="WpfPropertyGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"      
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <my:View />
    </Grid>
</Window>


The View.xaml:

XML
<UserControl x:Class="WpfPropertyGrid.View"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:xceedPg="clr-namespace:Xceed.Wpf.Toolkit.PropertyGrid;assembly=Xceed.Wpf.Toolkit"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <xceedPg:PropertyGrid SelectedObject="{Binding}"/>
    </Grid>
</UserControl>


View.cs:

C#
/// <summary>
/// Interaction logic for View.xaml
/// </summary>
public partial class View : UserControl
{
    public View()
    {
        InitializeComponent();

        this.DataContext = new ViewModel();
    }
}


ViewModel.cs:

C#
public class ViewModel
{
    public ViewModel()
    {
        FirstName = "Shai";
        LastName = "Vashdi";
    }

    [Category("Information")]
    [DisplayName("First Name")]
    [Description("This property uses a TextBox as the default editor.")]
    public string FirstName { get; set; }

    [Category("Information")]
    [DisplayName("Last Name")]
    [Description("This property uses a TextBox as the default editor.")]
    public string LastName { get; set; } 
}


I Hope It helps :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900