WPF 视图-模型视图绑定 需要帮助,请
我一直在玩弄并寻找如何将模型视图绑定到视图,但我似乎无法解决它。 我有一个名为“搜索”的视图,我想将其绑定到 SearchModelView。 视图有一个按钮和一个文本框,看起来:
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<ComboBox Height="23" HorizontalAlignment="Left" Margin="12,40,0,0" Name="comboBox1" VerticalAlignment="Top" Width="174" />
<Label Content="Client:" Height="28" HorizontalAlignment="Left" Margin="0,12,0,0" Name="label1" VerticalAlignment="Top" Width="71" />
<Label Content="Client Reference:" Height="28" HorizontalAlignment="Left" Margin="0,69,0,0" Name="label2" VerticalAlignment="Top" Width="117" />
<TextBox
x:Name="clientRefTxt"
Text="{Binding Path=ClientRef, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
Height="23"
HorizontalAlignment="Left"
Margin="12,103,0,0"
VerticalAlignment="Top"
Width="174" />
<Button
Content="Search Debtors"
Height="23"
HorizontalAlignment="Left"
Margin="12,140,0,0"
Name="button1"
VerticalAlignment="Top"
Width="89"
Command="{Binding Path=SearchCommand}"/>
</Grid>
我希望它绑定到 SearchViewModel:
命名空间 Master.ViewModel {
public class SearchViewModel:WorkspaceViewModel
{
RelayCommand _searchCommand;
readonly Search _search;
#region Search Properties
public string ClientRef
{
get { MessageBox.Show("GET CLIENTREF"); return _search.ClientRef; }
set
{
MessageBox.Show("SET CLIENTREF");
if (value == _search.ClientRef)
return;
_search.ClientRef = value;
base.OnPropertyChanged("ClientRef");
}
}
#endregion
public ICommand SearchCommand
{
get
{
MessageBox.Show("SEARCHCOMMAND");
if (_searchCommand == null)
{
_searchCommand = new RelayCommand(
param=> this.Search(),
param=> this.CanSearch
);
}
return _searchCommand;
}
}
public void Search()
{
MessageBox.Show("SEARCHING");
}
bool CanSearch
{
get { return true; }
}
}
}
我删除了顶部的所有程序集,但假设它们都在那里。另请注意,SearchViewModel 位于单独的 dll 中,而不是与视图一起位于 exe 中。 任何帮助都会很棒,或者至少是一个指向写入方向的指针,我已经阅读了有关 MVVM 的 msdn 文章,但这没有帮助...我需要一个更好的概述来绑定这些部分。 提前致谢。 聚苯乙烯 更多细节: SearchViewModel 属于 Master.ViewModel SearchView 是 GUI.View 的一部分 我知道绑定对象是如何工作的,但我不确定如何将视图绑定到视图模型
I have been playing around and looking around on how to Bind a modelview to a view, but i cant seem to work it out.
I have a view called Search and I want to bind it to SearchModelView.
View has one button and one textbox and looks:
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<ComboBox Height="23" HorizontalAlignment="Left" Margin="12,40,0,0" Name="comboBox1" VerticalAlignment="Top" Width="174" />
<Label Content="Client:" Height="28" HorizontalAlignment="Left" Margin="0,12,0,0" Name="label1" VerticalAlignment="Top" Width="71" />
<Label Content="Client Reference:" Height="28" HorizontalAlignment="Left" Margin="0,69,0,0" Name="label2" VerticalAlignment="Top" Width="117" />
<TextBox
x:Name="clientRefTxt"
Text="{Binding Path=ClientRef, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
Height="23"
HorizontalAlignment="Left"
Margin="12,103,0,0"
VerticalAlignment="Top"
Width="174" />
<Button
Content="Search Debtors"
Height="23"
HorizontalAlignment="Left"
Margin="12,140,0,0"
Name="button1"
VerticalAlignment="Top"
Width="89"
Command="{Binding Path=SearchCommand}"/>
</Grid>
And I want it to bind to SearchViewModel:
namespace Master.ViewModel
{
public class SearchViewModel:WorkspaceViewModel
{
RelayCommand _searchCommand;
readonly Search _search;
#region Search Properties
public string ClientRef
{
get { MessageBox.Show("GET CLIENTREF"); return _search.ClientRef; }
set
{
MessageBox.Show("SET CLIENTREF");
if (value == _search.ClientRef)
return;
_search.ClientRef = value;
base.OnPropertyChanged("ClientRef");
}
}
#endregion
public ICommand SearchCommand
{
get
{
MessageBox.Show("SEARCHCOMMAND");
if (_searchCommand == null)
{
_searchCommand = new RelayCommand(
param=> this.Search(),
param=> this.CanSearch
);
}
return _searchCommand;
}
}
public void Search()
{
MessageBox.Show("SEARCHING");
}
bool CanSearch
{
get { return true; }
}
}
}
I removed all the assemblies at the top but assume that they are all there. Also note that SearchViewModel is in a separate dll, not in the exe with the View.
Any help would be great or at least a pointer in the write direction, I have already read the msdn article on MVVM and that didnt help...I kinda need a better rundown on binding those too pieces.
Thanks in Advance.
P.S.
Some more details:
SearchViewModel belongs to Master.ViewModel
SearchView is part of GUI.View
I have and idea how the binded objects work, im not to sure on how to bind the view to the viewmodel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的视图是网格吗?我只使用 UserControl 或 Window 类型作为视图,但您可能会成功使用网格。
无论如何,这是使用 UserControl 视图实例化 ViewModel 的最简洁方法。如果您使用的是网格,只需将 UserControl 标记替换为网格标记即可。
我相信,除非必要,否则远离视图代码是 MVVM 的首选模式 - 让 XAML 在可能的情况下为您连接起来。
Is your View a Grid? I've only used UserControl or Window types as Views, but you may have success using a Grid.
Regardless, this is the cleanest way to instantiate the ViewModel with a UserControl View. Just replace the UserControl tags with Grid tags if you're using a Grid.
I believe keeping out of the View's code unless necessary is the preferred pattern for MVVM - let the XAML wire things up for you when possible.
您需要将视图的 DataContext 设置为视图模型的实例。有多种方法可以执行此操作,包括自动连接它的框架,但最简单的开始方法是在视图的构造函数中执行此操作:
显然,您可能需要提供其他信息来初始化 SearchViewModel 但希望这足以让你走上正轨。
You need to set the view's DataContext to an instance of the view model. There are a variety of ways of doing this, including frameworks that wire it up automagically, but the easiest way to get started is to do it in the constructor of the view:
Obviously you may need to provide other information to initialise the SearchViewModel but hopefully this is enough to get you on the right track.
您需要像@itowlson 建议的那样引导您的应用程序。
但是,如果您有多个 ViewModel,您应该允许 WPF 为您做这件事。执行此操作的基本方法(在您开始拥有十几个视图之前很容易维护)是创建一个 DataTemplate 将视图与模型视图(大多数人称为 ViewModel)联系起来。
因此,您提供的 xaml 可能位于 UserControl 中(至少应该是),因此您需要做几件事
首先创建一个 ResourceDictionary
(快速方法是右键单击您的项目,然后单击
Add -> Resource Dictionary
在该文件中(让我们将其命名为
Resources.xaml
):上面假设您分别为 View 和 ViewModel 命名空间放置命名空间
vw
和vm
转到您的
App.xaml
并输入以下内容:上面将告诉 WPF每当遇到
SearchViewModel
类型的对象时,即可:SearchView
对象SearchViewModel
对象HTH
Your will need to bootstrap your application like @itowlson suggests.
But if you have more than one ViewModel you should allow WPF to do it for you. The basic way to do this (which is easy to maintain until you start having more than a dozen views) is to create a DataTemplate to tie the View with your ModelView(which most people call ViewModel).
So the xaml you provided is probably in a UserControl(at least it should be) so you need to do several things
First create a ResourceDictionary
(fast way is to right-click your project and click
Add -> Resource Dictionary
In that file(let's name it
Resources.xaml
) put this :The above is assuming you put the namespaces
vw
andvm
for View and ViewModel namespaces respectivelyGo to your
App.xaml
and put this:The above will tell WPF that whenever it encounters an object of type
SearchViewModel
to:SearchView
objectSearchViewModel
objectHTH