1

タイトルを見ると簡単そうに見えますが、かなりトリッキーです。ボタン、ラベル、テキストボックスを動的に生成する必要がある wpf アプリを開発しています。VoltageView XAml ファイルで、stackpanel を作成しました。VoltageChannelView xaml ファイルで、すべての UI コンポーネントを作成しました。

私は次のようにある程度達成しました:

電圧ビュー:

<Grid Grid.Row="1" Style="{DynamicResource styleBackground}" Name="VoltageChannels" >
        <StackPanel Height="Auto" Name="stackPanel" Width="Auto" MinHeight="300"></StackPanel>
</Grid>

VoltageChannelView :

<Label Grid.Column="0" Content="{Binding ChannelName}" />
<TextBox Grid.Column="1" Text="{Binding VoltageText}" />
<Button Grid.Column="1" Content="Set" CommandParameter="{Binding VoltageText}" Command="{Binding VoltageCommand}" />

上記の動的に生成された Ui コンポーネントを、次のようにVoltageView.xaml.csのスタックパネルに追加しています。

VoltageViewModel mVoltageViewModel = new VoltageViewModel();

// Called in constructor
public void OnChildAdd()
    {            
        foreach (VoltageBoardChannel mVoltageChannelViewModel in mVoltageViewModel.VoltageChannelList)
        {
            VoltageChannelView mVoltageChannelView = new VoltageChannelView();
            mVoltageChannelView.Margin = new Thickness(2);
            mVoltageChannelView.ChannelInfo = mVoltageChannelViewModel;
            stackPanel.Children.Add(mVoltageChannelView);
        }
    }

VoltageViewModel クラス:

public ObservableCollection<VoltageBoardChannel> channelList = null;

    public ObservableCollection<VoltageBoardChannel> redhookChannels = new ObservableCollection<VoltageBoardChannel>
    {             
         new VoltageBoardChannel { ChannelName = "VDD_IO_AUD", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD_CODEC_AUD", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD_DAL_AUD", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD_DPD_AUD", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD_PLL_AUD", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD_AMP1_AUD", IsAvailable = true}             
    };       

    public ObservableCollection<VoltageBoardChannel> bavaria1Channels = new ObservableCollection<VoltageBoardChannel>
    {
         new VoltageBoardChannel { ChannelName = "VDD__MAIN", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD__IO", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD__CODEC", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD__LDO", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD__AMP", IsAvailable = true},  

    };        

    public VoltageViewModel()
    {
        channelList = new ObservableCollection<VoltageBoardChannel>();
        channelList = bavaria1Channels;          

    }

    public ObservableCollection<VoltageBoardChannel> VoltageChannelList
    {
        get 
        { 
            return channelList; 
        }

        set
        { 
            channelList = value;
            OnPropertyChanged("ChannelList");
        }
    }

    RelayCommand _voltageCommand;
    public ICommand VoltageCommand
    {
        get
        {
            if (_voltageCommand == null)
            {
                _voltageCommand = new RelayCommand(param => this.DoSomethingExecute, param => this.DoSomethingCanExecute);
            }
            return _voltageCommand;
        }
    }

    public bool DoSomethingCanExecute(object param)
    {
        return true;
    }

    public void DoSomethingExecute(object param)
    {

    }

起動時にわかるように、BAVARIA1チャンネルが表示されます。

VoltageBoardChannel (モデル)クラス:

private string mChannelName;
    public string ChannelName
    {
        get
        {
            return mChannelName;
        }
        set
        {
            mChannelName = value;
            OnPropertyChanged("ChannelName");
        }
    }

    private bool mIsAvailable;
    public bool IsAvailable
    {
        get; set;
    }

    string voltageText = string.Empty;
    public string VoltageText
    {
        get
        {
            return voltageText;
        }

        set
        {
            voltageText = value;
            OnPropertyChanged("VoltageText");
        }
    }      

したがって、アプリを実行すると、リストに保持されているように、bavaria1 チャンネルが 4 回動的に表示されます。動的に生成された各コントロールには、テキストボックスとボタンがあります。

VoltageChannelView.xamlbw Button と textbox のバインドを行ったことがわかります。テキストボックスに値を入力したいのですが、SETボタンをクリックすると、入力された値を取得して、さらに操作を実行できるようにする必要があります。基本的には、テキストボックスに書かれたテキストをパラメータとして渡すSETボタンのイベントが必要です。

どうすれば達成できますか?:)

4

1 に答える 1

1

私がこれを見たとき、あなたはあなたのコマンドの実装を見逃しています。必要なのは Commands を処理するロジックです。

この記事のソースをコピーModel-View-ViewModel デザイン パターンを使用した WPF アプリ

RelayCommand クラス

public class RelayCommand : ICommand
{
    #region Fields

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;        

    #endregion // Fields

    #region Constructors

    public RelayCommand(Action<object> execute)
    : this(execute, null)
    {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;           
    }
    #endregion // Constructors

    #region ICommand Members

    [DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    #endregion // ICommand Members
}

コマンドの実装は次のようになります。

RelayCommand _voltageCommand ;
public ICommand VoltageCommand 
{
    get
    {
        if (_voltageCommand == null)
        {
            _voltageCommand = new RelayCommand(this.DoSomethingExecute,
                this.DoSomethingCanExecute);
        }
        return _voltageCommand;
    }
}

実行方法:

public void DoSomethingExecute(object param)
{
    // param is your string        
    //Do Something with VoltageText
}

CanExecute メソッド

public bool DoSomethingCanExecute(object param)
{
    return true;        
}
于 2012-10-16T08:05:30.220 に答える