タイトルを見ると簡単そうに見えますが、かなりトリッキーです。ボタン、ラベル、テキストボックスを動的に生成する必要がある 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.xaml
bw Button と textbox のバインドを行ったことがわかります。テキストボックスに値を入力したいのですが、SETボタンをクリックすると、入力された値を取得して、さらに操作を実行できるようにする必要があります。基本的には、テキストボックスに書かれたテキストをパラメータとして渡すSETボタンのイベントが必要です。
どうすれば達成できますか?:)