2012-10-15 48 views
1

我正在處理股票交易者參考實施的示例代碼。如何將命令行爲附加到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" /> 
+0

我剛剛找到解決方案和它的簡單。但我希望你贏得200分!只需看一下文本框的編輯代碼即可。特別是在基礎設施:ReturnKey.Command。 –

回答

1

我下載的源。 TextBox似乎已經消失。將TextBox的命令更改爲DataContext.BuyCommand「修復」「問題」。命令「UpdateTickerSymbolCommand」也似乎不再可用,所以我無法對此進行測試。當前源也沒有對StackPanel(LayoutRoot)的引用,所以這也是我無法跟蹤的。我認爲這裏的關鍵問題是命令沒有正確設置。

1

您可以簡單地使用TextBox.InputBindings,你想模式=雙向和UpdateSourceTrigger =的PropertyChanged文本。

的.xaml

<ListView HorizontalAlignment="Stretch" 
       Height="Auto" 
       ItemsSource="{Binding Collection}" 
       VerticalAlignment="Stretch" 
       Width="Auto"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <TextBox Width="100" 
         Text="{Binding MyText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 
        <TextBox.InputBindings> 
         <KeyBinding Key="Enter" Command="{Binding MyCommand}" /> 
        </TextBox.InputBindings> 
       </TextBox> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

該數據模型將

。CS

public class DataModel : INotifyPropertyChanged 
{ 
    private string m_myText; 
    public string MyText 
    { 
     get { return m_myText; } 
     set 
     { 
      m_myText = value; 
      OnPropertyChanged("MyText"); 
     } 
    } 

    public ICommand MyCommand { get; set; } 

    public event PropertyChangedEventHandler PropertyChanged; 

    public void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

收集關於視圖模型

ViewModel.cs

Collection = new ObservableCollection<DataModel> { new DataModel() { MyText = String.Empty, MyCommand = m_myCommand } }; 
+0

這有效,但我必須瞭解棱鏡中的附加行爲是如何工作的。 –

相關問題