1

株式トレーダーのリファレンス実装のサンプルコードを使用しています。

http://prism4.googlecode.com/svn/trunk/Prism4/

PositionSummaryView.xamlに株式のリストがあります。株のリストに、株の名前を表示するテキストボックスを含むGridViewColumnを追加しました。ユーザーがReturnCommandBehaviorの動作を使用してエンターキーを押したときに、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>

2回目の試行

  <TextBox Grid.Column="0"  Text="{Binding  Path=TickerSymbol}"   Infrastructure:ReturnKey.Command="{Binding ElementName=root,Path= UpdateTickerSymbolCommand}" ></TextBox>

ビューモデルは次のとおりです。

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" />
4

2 に答える 2

1

ソースをダウンロードしました。TextBoxが消えたようです。TextBoxのコマンドをDataContext.BuyCommandに変更すると、「問題」が「修正」されました。コマンド「UpdateTickerSymbolCommand」も使用できなくなったようで、これもテストできませんでした。現在のソースにもStackPanel(LayoutRoot)への参照がないため、これも追跡できませんでした。ここでの重要な問題は、コマンドが正しく設定されていないことだと思います。

于 2012-10-22T17:54:19.447 に答える
1

単純に TextBox.InputBindings を使用でき、テキストに Mode=TwoWay および 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 } };
于 2012-10-16T03:23:30.047 に答える