简体   繁体   English

WPF绑定混乱

[英]Wpf Binding Confusion

I'm trying to learn on MVVM. 我正在尝试学习MVVM。 I've understand the concept, however, i'm confused about the binding. 我了解这个概念,但是我对绑定感到困惑。 I'm not sure where to bind my Fill property. 我不确定在哪里绑定我的Fill属性。 Please help. 请帮忙。 Tqvm in advanced. Tqvm高级。

View - name: MainScreen.xaml 视图-名称:MainScreen.xaml

<Path Fill="{Binding mainScreenClass, Converter={StaticResource colorConverter}}"/>

inCodeBehind inCodeBehind

DataContext = new vmMainScreen();

ViewModel - name:vmMainScreen ViewModel-名称:vmMainScreen

public ICommand cmdMouseEnterNav { get; private set; }
public mMainScreen mainScreenClass { get; set; }
public vmMainScreen()
{
    mainScreenClass = new mMainScreen();
    mainScreenClass.propNaviconFill = new SolidColorBrush(Colors.White);
    naviconMouseEventChecker();
}

private void naviconMouseEventChecker()
{
    cmdMouseEnterNav = new SimpleCommand
    {
        ExecuteDelegate = x => mainScreenClass.propNaviconFill = (SolidColorBrush)(new BrushConverter().ConvertFrom("#c5a02b"))
    };
}

Model - name:mMainScreen 型号-名称:mMainScreen

public class mMainScreen : INotifyPropertyChanged
{
    private Brush _NaviconFill = new SolidColorBrush(Colors.White);
    public Brush propNaviconFill
    {
        get
        {
            return this._NaviconFill;
        }
        set
        {
            this._NaviconFill = value;
            NotifyPropertyChanged("propNaviconFill");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

I understand that when i breakpoint on my colorConverter, I'm getting the class. 我了解到当我在colorConverter上断点时,我正在上课。 Not the property of propNaviconFill. 不是propNaviconFill的属性。 If i create another property with the Brush class on my ViewModel and bind it to Fill, there is no problem. 如果我在ViewModel上使用Brush类创建另一个属性并将其绑定到Fill,就没有问题。 But that means I'm not following the correct structure of MVVM. 但这意味着我没有遵循正确的MVVM结构。 Thanks again. 再次感谢。

You should bind to the property of your view model. 您应该绑定到视图模型的属性。

<Path Fill="{Binding propNaviconFill, Converter={StaticResource colorConverter}}"/>

Use the view model implementing the INotifyPropertyChanged as the data context of your view. 使用将INotifyPropertyChanged实现为视图的数据上下文的视图模型。

DataContext = new mMainScreen();

If you really want to use vmMainScreen as your data context, then vmMainScreen should implement INotifyPropertyChanged there and you should study how NotifyPropertyChanged was used to notify the view that the view model property has changed. 如果您确实要使用vmMainScreen作为数据上下文,则vmMainScreen应该在那里实现INotifyPropertyChanged ,并且您应该研究如何使用NotifyPropertyChanged通知视图视图模型属性已更改。

Keep in mind there are two basic types of MVVM: 1. View First 2. View Model First 请记住,MVVM有两种基本类型:1.首先查看2.首先查看模型

Based on your example you are trying to do View First. 根据您的示例,您尝试执行“视图优先”。 This is easier to implement but comes with drawbacks on larger projects since the view controls the creation of the ViewModel it's harder to inject data or state into the ViewModel. 这易于实现,但在较大的项目中也有缺点,因为视图控制着ViewModel的创建,因此很难将数据或状态注入到ViewModel中。

For all MVVM patterns you have the three parts: 对于所有MVVM模式,您可以分为三个部分:

Model - Basically a state bag. 模型 -基本上是一个状态包。 This thing is just like a customer class which most of the time implements INotifyProperty changed. 这件事就像大多数情况下实现INotifyProperty更改的客户类。

ViewModel - This is like the controller class in MVC. ViewModel-类似于MVC中的控制器类。 It has all the real logic and does the work. 它具有所有真实的逻辑并可以正常工作。

View - This is your XAML and it only holds presentation logic. 视图 -这是您的XAML,仅包含表示逻辑。 The code-behind class ie: MyWindow.xaml.cs should not be used except to setup the ViewModel if your going View First. 如果要使用View First,则不应使用代码隐藏类(即MyWindow.xaml.cs),除非要设置ViewModel。 (there are exceptions of course but generally it should basically be empty) (当然也有例外,但通常基本上应该是空的)

For View First Your Window (or control) should create the ViewModel in the constructor and assign it to the DataContext. 对于“首先查看”,您的窗口(或控件)应在构造函数中创建ViewModel并将其分配给DataContext。

Your ViewModel will have ICommand's, ObservableCollections and such that can be bound to controls in the View. 您的ViewModel将具有ICommand的ObservableCollections,并且可以绑定到View中的控件。 So when your constructor fires you fill up your data and put it into the necessary structures; 因此,当您的构造函数触发时,您会填充数据并将其放入必要的结构中; because of databinding this gets related to the View and shown. 由于数据绑定,它与视图相关并显示。

Your Model (you usually have more than one, can have Customer, Order, StockTicker or whatever.) These are created by the ViewModel and put into things such as ObservableCollections for the View to databind to. 您的模型(通常有多个模型,可以有Customer,Order,StockTicker或其他任何东西。)这些模型是由ViewModel创建的,并放入诸如ObservableCollections之类的东西供View进行数据绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM