在我的WPF MVVM項目中,我有一個按鈕觸發一個函數,該函數應該將節點添加到xml,然後將焦點設置爲文本框。 我的問題是,我如何獲得對控件的引用?如何接收對WPF(MVVM)中控件的引用?
查看:
<Button Command="{Binding Path=ButtonAddCategory_Click}" />
視圖模型:
RelayCommand buttonAddCategory_Click;
public ICommand ButtonAddCategory_Click
{
get
{
return buttonAddCategory_Click ?? (buttonAddCategory_Click = new RelayCommand(param => this.AddCategory(),
param => true));
}
}
public void AddCategory()
{
...
//get the "node" -> reference?
XmlNode selectedItem = (XmlNode)treeView.SelectedItem;
..
//add the node to the xml
..
//change focus -> reference?
textBoxTitel.Focus();
textBoxTitel.SelectAll();
}
TreeViews對MVVM不好用,不幸... – Will 2011-04-08 14:02:54
@實際上它們*可以很好地與MVVM一起玩,但它需要一點工作......我使用一個附加的行爲使得它相對容易綁定SelectedItem。請參閱Josh Smith的[本文](http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx) – 2011-04-08 14:11:30
@ThomasLevesque:lurl,使我做了一些工作來獲得其他控件附帶的相同功能開箱即使打得不錯, – Will 2011-04-08 14:31:56