好吧,我是一名 C++ 开发人员,目前我正在开发 WPF 应用程序,看起来这是一个棘手的情况。我已经动态生成了一组按钮、标签等,其中文本框和按钮都相互绑定。我之前在 C++ 代码中完成了此操作,现在我需要在 WPF 应用程序中执行此操作。
XAML:
<ListBox x:Name="myViewChannelList" HorizontalAlignment="Stretch" Height="Auto" ItemsSource="{Binding VoltageCollection}" Margin="0" VerticalAlignment="Stretch" Width="Auto" >
<ListBox.Resources>
<convert:BooleanToVisibilityConverter x:Key="booltovisibility"/>
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate >
<Grid Visibility="{Binding IsAvailable, Converter={StaticResource booltovisibility}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="170" />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="{Binding ChannelName}" Margin="50,20,0,0"></Label>
<Grid Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Text="{Binding VoltageText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="25" Width="50" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="170,20,0,0" />
<Button Grid.Column="1" Content="Set" Height="25" CommandParameter="{Binding VoltageText}" Command="{Binding VoltageCommand}" Width="50" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="20,20,0,0" ></Button>
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
视图模型:
private ICommand m_voltageCommand;
public ChannelList()
{
m_voltageCommand = new DelegateVoltageCommand(x => SetCommandExecute(x));
}
public void Initialize()
{
VoltageCollection = new ObservableCollection<VoltageModel> { new VoltageModel() { ChannelName = "", IsAvailable = false, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
new VoltageModel() { ChannelName = "VDD__Main", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
new VoltageModel() { ChannelName = "VDD__IO__AUD", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
new VoltageModel() { ChannelName = "VDD__CODEC__AUD", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand }
};
}
ObservableCollection<VoltageModel> _voltages;
public ObservableCollection<VoltageModel> VoltageCollection
{
get
{
return _voltages;
}
set
{
_voltages = value;
OnPropertyChanged("VoltageCollection");
}
}
// Event when SET Button is clicked
public void SetCommandExecute(object voltageText)
{
string value = voltageText.ToString();
int val = Convert.ToInt32(value);
}
因此,它会生成 Button + Textbox + Label 3 次,如Initialize()
方法所示。现在VoltageCommand = m_voltageCommand
给我在文本框中输入的文本,它调用电压文本SetCommandExecute(object voltageText)
给我输入值的方法。
模型:
string voltageText = string.Empty;
public string VoltageText
{
get
{
return voltageText;
}
set
{
voltageText = value;
OnPropertyChanged("VoltageText");
}
}
**C++ Code:**
// Since we have 3 channels, channel maintains count
if(button == m_setButton[channel])
{
unsigned cmd = 0x0300;
int numBytes = 0;
cmd |= (channel & 0xFF);
// Some code
在这里,它告诉用户哪个按钮已被单击,并采用channe
l 的值,即如果单击第二个按钮则channel = 2
。
在这里,我需要实现用 C++ 编写的代码。如何获取频道,即点击了哪个按钮。看看cmd |= (channel & 0xFF);
,它使用了channel
值。如何在我的应用程序中实现它?