MVVM体系结构中的属性网格绑定 [英] Property Grid Binding in MVVM Architecture

查看:109
本文介绍了MVVM体系结构中的属性网格绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在通过MVVM架构转换我的WPF应用程序。我的应用程序中有一个xaml,它使用Property Grid并使用它显示数据。现在对于MVVM架构,我必须通过绑定来完成所有这些。我已经阅读了大约100多篇文章,但没有得到任何适当的例子来说明这个或者在MVVM架构中使用Property Grid。



请紧急帮我解决这个问题。我需要一个简短的演示,它使用MVVM架构并将数据绑定到Property Grid。

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.

推荐答案

Hi Shashank,



也许你不需要MVVM。只有在编写Business-Logic + UI并且想要将它们分开时才应该使用MVVM。



如果您正在使用UI-Logic编写控件不需要使用MVVM!



例如,假设我创建了一个Property Grid控件,它通过他的属性反映了一些对象。 Property Grid控件公开了这个对象的Dependency Property(让我们称之为ObjectSource),这样我们可以稍后在XAML上绑定到这个属性。





如果对象是Business-Logic对象,则创建一个包含Property Grid控件的User-Control(视图)。您只需轻松地将View-Model或其中一个属性绑定到Property Grid控件ObjectSource(依赖属性)。



试试这个,并且如果你还有其他问题请在这里写下代码,我会指导你扔掉它。



最好的问候,



Shai
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


Hi Shashank,



感谢您的评论:)



我冒昧地写了一个简单的样式MVVM View-First使用Extended WPF Toolkit PropertyGrid:



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



MainWindow.cs:



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:

<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>





View.xaml:





The View.xaml:

<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 :





View.cs:

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

        this.DataContext = new ViewModel();
    }
}





ViewModel.cs:





ViewModel.cs:

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 :)


这篇关于MVVM体系结构中的属性网格绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆