我從字面上從來沒有使用WPF之前,只有WinForms和我試圖遵循this tutorial,但示例代碼真的很糟糕。不完整的片段和很多留給讀者來推斷一切屬於哪裏。如何將控件綁定到對象屬性?
它在談論添加數據綁定和此C#代碼進行比較:
using CustomerViewModel;
對此XAML代碼:
xmlns:custns="clr-namespace:CustomerViewModel;assembly=CustomerViewModel"
但無處這篇文章似乎表明其中這應該走。在它下面,它說你不需要實際編寫綁定代碼,而是可以在Visual Studio中使用UI/Property表,但VS2010看起來不像這些截圖,所以我試圖這樣做。
不管怎麼說,這是我在XAML我的應用程序只有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}">
此代碼不但是編譯,並給我這個錯誤:
A using namespace directive can only be applied to namespaces;
NYMS_Rewrite.view_models.FormProcessorViewModel
is a type not a namespace;
如果我雙擊錯誤,它需要我到MainWindow.g.cs
(這到底是什麼!?它不顯示在我的解決方案資源管理器中)
而第一行是th是using
聲明:
using NYMS_Rewrite.view_models.FormProcessorViewModel;
我試圖縮短到僅僅using NYMS_Rewrite.view_models;
但只要我編譯它可以追溯到它是什麼。我假設這是由我的XAML生成的,但即使我將其更改爲僅clr-namespace:NYMS_Rewrite.view_models;assembly=NYMS_Rewrite.view_models
我仍然得到相同的結果。
而那類被定義爲這樣:
namespace NYMS_Rewrite.view_models
{
class FormProcessorViewModel
{
// stuff
}
}
所以我不知道爲什麼它認爲這是一個無效的命名空間。我想要做的就是將幾個文本框綁定到我的視圖模型的字符串屬性。
編輯固定,工作XAML與方炯鑌的回答是:
<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>
刪除'的xmlns:custns = 「CLR-名稱空間:NYMS_Rewrite.view_models.FormProcessorViewModel;裝配= NYMS_Rewrite.view_models.FormProcessorViewModel」'從XAML –
的 「.G」 在MainWindow.g.cs代表 「生成」 –
@AbinMathew我不需要這樣的設計器,我可以在'Data Binding'屬性/配置工具中找到一個'FormProcessorViewModel'對象? – sab669