我正在處理股票交易者參考實施的示例代碼。如何將命令行爲附加到ListView中的TextBox中?
http://prism4.googlecode.com/svn/trunk/Prism4/
有一個在PositionSummaryView.xaml股票列表。在股票清單中,我添加了一個帶有顯示股票名稱的文本框的GridViewColumn。當用戶通過使用ReturnCommandBehavior行爲命中enterkey時,我嘗試在ViewModel中調用命令。
當我在文本框中按Enter鍵時命令未被命中。當我在列表之外有相同的文本框時,命令被擊中。
這工作ListView控件
<TextBox Grid.Column="0" Text="test" Infrastructure:ReturnKey.Command="{Binding Path=UpdateTickerSymbolCommand}" ></TextBox>
這外面是我在TextBox試圖綁定,但沒有工作:
<TextBox Grid.Column="0" Text="{Binding Path=TickerSymbol}"
Infrastructure:ReturnKey.Command="{Binding Path=UpdateTickerSymbolCommand, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}" ></TextBox>
第二次嘗試
<TextBox Grid.Column="0" Text="{Binding Path=TickerSymbol}" Infrastructure:ReturnKey.Command="{Binding ElementName=root,Path= UpdateTickerSymbolCommand}" ></TextBox>
這裏是viewmodel:
using System.ComponentModel.Composition;
using System.Windows.Input;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.ViewModel;
using StockTraderRI.Infrastructure;
using StockTraderRI.Modules.Position.Controllers;
using Microsoft.Practices.Prism.Commands;
namespace StockTraderRI.Modules.Position.PositionSummary
{
[Export(typeof(IPositionSummaryViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class PositionSummaryViewModel : NotificationObject, IPositionSummaryViewModel
{
private PositionSummaryItem currentPositionSummaryItem;
private readonly IEventAggregator eventAggregator;
public IObservablePosition Position { get; private set; }
private ICommand updateTickerSymbolCommand;
public ICommand UpdateTickerSymbolCommand { get { return this.updateTickerSymbolCommand; } }
[ImportingConstructor]
public PositionSummaryViewModel(IOrdersController ordersController, IEventAggregator eventAggregator, IObservablePosition observablePosition)
{
this.eventAggregator = eventAggregator;
this.Position = observablePosition;
BuyCommand = ordersController.BuyCommand;
SellCommand = ordersController.SellCommand;
updateTickerSymbolCommand = new DelegateCommand<string>(this.UpdateTickerSymbol); ;
this.CurrentPositionSummaryItem = new PositionSummaryItem("FAKEINDEX", 0, 0, 0);
}
private void UpdateTickerSymbol(string tickerSymbol)
{
}
public ICommand BuyCommand { get; private set; }
public ICommand SellCommand { get; private set; }
public string HeaderInfo
{
get { return "POSITION"; }
}
public PositionSummaryItem CurrentPositionSummaryItem
{
get { return currentPositionSummaryItem; }
set
{
if (currentPositionSummaryItem != value)
{
currentPositionSummaryItem = value;
this.RaisePropertyChanged(() => this.CurrentPositionSummaryItem);
if (currentPositionSummaryItem != null)
{
eventAggregator.GetEvent<TickerSymbolSelectedEvent>().Publish(
CurrentPositionSummaryItem.TickerSymbol);
}
}
}
}
}
}
當按下Enter鍵時,我需要點擊UpdateTickerSymbol?
編輯
我現在看到了,我在一些示例代碼曲解
Binding ElementName=root
。我認爲根本是一個關鍵字,但它是有,爲什麼我的父母控制研究使用
<StackPanel x:Name="LayoutRoot" >
現在必須考慮到家長控制視圖
那鑰匙,
<TextBox Grid.Column="0" Text="{Binding Path=TickerSymbol}" Infrastructure:ReturnKey.Command="{Binding ElementName=LayoutRoot, Path=UpdateTickerSymbolCommand}" ></TextBox>
爲TextBox,但命令仍然沒有命中。
我也嘗試了上面的語法與樣品中一個按鈕,它的工作
<Button Grid.Column="0" Command="{Binding Path=DataContext.BuyCommand, ElementName=LayoutRoot}" CommandParameter="{Binding Path=TickerSymbol}" AutomationProperties.AutomationId="ActionsBuyButton" Template="{StaticResource AddButtonTemplate}" Cursor="Hand" Width="30" />
我剛剛找到解決方案和它的簡單。但我希望你贏得200分!只需看一下文本框的編輯代碼即可。特別是在基礎設施:ReturnKey.Command。 –