How to bind control to object property?

sab669

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

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to bind control to object property?

From Dev

How to parse a json object property to bind it to a control

From Dev

How to bind user control property to other property?

From Dev

How to bind a property of control to another control's property

From Dev

How to bind a property of control to another control's property

From Dev

How to Bind to Another Control Property in WPF

From Dev

How to bind data to a control's Visibility property

From Dev

How to bind data to a control's Visibility property

From Dev

How to bind to a control's property which is inside control template?

From Dev

How to bind a property to the property of another class (with no UI Control)?

From Dev

How to bind a property to the property of another class (with no UI Control)?

From Dev

Bind control to escaped property?

From Dev

How to bind property on parent object with knockoutjs?

From Dev

How to bind a list which is a property of an object to DataGridView?

From Dev

How to bind template to object in array by property value?

From Dev

How to bind List object with no duplicate property

From Dev

How to Bind an another Control on the Setter Value Property by using a Trigger?

From Dev

How to bind custom dependecy property to control's view model?

From Dev

How to programmatically bind a (dependency) property of a control that's inside a DataTemplate?

From Dev

How to bind custom dependecy property to control's view model?

From Dev

How do I bind a WPF control's margin to a property?

From Dev

How to bind a property to a singleton object property from QML

From Dev

Bind to property of object

From Dev

JavaFX bind not property member to control

From Dev

Bind a property to another property of a custom control

From Dev

How to bind a string variable to datepicker control instead of a date object?

From Dev

How to bind a property of a user control to the MainViewModel and bind its datacontext to its own viewmodel?

From Dev

How to data-bind control's property within ControlTemplate, to the data-context of the window that contains the control with this template?

From Dev

(Windows 10 UWP) How to bind Header property in PivotItem control into custom HeaderTemplate in Pivot control?

Related Related

  1. 1

    How to bind control to object property?

  2. 2

    How to parse a json object property to bind it to a control

  3. 3

    How to bind user control property to other property?

  4. 4

    How to bind a property of control to another control's property

  5. 5

    How to bind a property of control to another control's property

  6. 6

    How to Bind to Another Control Property in WPF

  7. 7

    How to bind data to a control's Visibility property

  8. 8

    How to bind data to a control's Visibility property

  9. 9

    How to bind to a control's property which is inside control template?

  10. 10

    How to bind a property to the property of another class (with no UI Control)?

  11. 11

    How to bind a property to the property of another class (with no UI Control)?

  12. 12

    Bind control to escaped property?

  13. 13

    How to bind property on parent object with knockoutjs?

  14. 14

    How to bind a list which is a property of an object to DataGridView?

  15. 15

    How to bind template to object in array by property value?

  16. 16

    How to bind List object with no duplicate property

  17. 17

    How to Bind an another Control on the Setter Value Property by using a Trigger?

  18. 18

    How to bind custom dependecy property to control's view model?

  19. 19

    How to programmatically bind a (dependency) property of a control that's inside a DataTemplate?

  20. 20

    How to bind custom dependecy property to control's view model?

  21. 21

    How do I bind a WPF control's margin to a property?

  22. 22

    How to bind a property to a singleton object property from QML

  23. 23

    Bind to property of object

  24. 24

    JavaFX bind not property member to control

  25. 25

    Bind a property to another property of a custom control

  26. 26

    How to bind a string variable to datepicker control instead of a date object?

  27. 27

    How to bind a property of a user control to the MainViewModel and bind its datacontext to its own viewmodel?

  28. 28

    How to data-bind control's property within ControlTemplate, to the data-context of the window that contains the control with this template?

  29. 29

    (Windows 10 UWP) How to bind Header property in PivotItem control into custom HeaderTemplate in Pivot control?

HotTag

Archive