简体   繁体   中英

How to bind control to object property?

I've literally never used WPF before, only WinForms and I'm trying to follow this tutorial , but the sample code is really just terrible. Incomplete snippets and a lot is left to the reader to infer where everything belongs.

It's talking about adding data binding and compares this C# code:

using CustomerViewModel;

To this XAML code:

xmlns:custns="clr-namespace:CustomerViewModel;assembly=CustomerViewModel"

But nowhere does this article seem to show where this is supposed to go. Below that it says you don't need to actually write binding code and can instead use the UI / Property sheet in Visual Studio, but VS2010 does not look anything like those screenshots so I'm trying to do it this way.

Anyways, this is what I have in the XAML for my application's only Window :

<Window x:Class="NYMS_Rewrite.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:custns="clr-namespace:NYMS_Rewrite.view_models.FormProcessorViewModel;assembly=NYMS_Rewrite.view_models.FormProcessorViewModel"
  Title="NYMS Logical Checks" Height="606" Width="525" Background="AliceBlue" DataContext="{Binding}">

This code does not compile, however, and is giving me this error:

A using namespace directive can only be applied to namespaces; NYMS_Rewrite.view_models.FormProcessorViewModel is a type not a namespace;

If I double click the error, it takes me to MainWindow.g.cs (what the hell is this!? It doesn't show in my Solution Explorer)

And the first line is this using statement:

using NYMS_Rewrite.view_models.FormProcessorViewModel;

I've tried shortening to just using NYMS_Rewrite.view_models; but as soon as I compile it goes back to what it was. I assume this is being generated by my XAML but even if I change that to just clr-namespace:NYMS_Rewrite.view_models;assembly=NYMS_Rewrite.view_models I still get the same results.

And that class is defined as so:

namespace NYMS_Rewrite.view_models
{
    class FormProcessorViewModel
    {
        // stuff
    }
}

So I don't know why it thinks it's an invalid namespace. All I wanted to do is bind a few textboxes to my view model's string properties.

EDIT Fixed, working XAML with Abin's answer:

<Window x:Class="NYMS_Rewrite.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:fpvm="clr-namespace:NYMS_Rewrite.view_models"
  Title="NYMS Logical Checks" Height="606" Width="525" Background="AliceBlue" DataContext="{Binding}">
    <Window.Resources>
        <fpvm:FormProcessorViewModel x:Key="fpvmObj" />
    </Window.Resources>

The error is because of bad namespace

try removing the xmlns:custns="clr-namespace:NYMS_Rewrite.view_models.FormProcessorViewModel;ass‌​embly=NYMS_Rewrite.view_models.FormProcessorViewModel" From XAML and specify correct namespace which points your ViewModel

The error is because the XAML compiler is not able to find out the namespace you specified. First you have to build your ViewModel Project.

For Example xmlns:Utility="clr-namespace:ecclient.viewer.Utility" xmlns = xml name space , Utility is the alias name you can use in XAML , clr-namespace: says that you are referring to a name space then your name space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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