【发布时间】:2012-10-15 17:26:48
【问题描述】:
好吧,我是一名 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
这里它告诉用户哪个按钮被点击并取channel的值,即如果第二个按钮被点击然后channel = 2。
这里我需要实现用 C++ 编写的代码。如何获取频道,即点击了哪个按钮。看看cmd |= (channel & 0xFF);,它使用了channel 值。如何在我的应用程序中实现它?
【问题讨论】:
-
我不确定我是否理解您想要做什么,但为什么不将整个
VoltageModel传递给命令而不是只传递文本?只需将CommandParameter="{Binding VoltageText}"更改为CommandParameter="{Binding }" -
为什么不只为按钮使用 Tag 属性?
button1.Tag = 1等 -
动态与否,它是一个框架元素属性。 :) 我对一些代码的 Tag 属性使用自定义类。 Tag property 是许多非常方便的框架元素属性之一。
-
@StonedJesus 如果您使用的是 MVVM 设计模式,那么您根本不应该引用 UI 对象。我不明白您为什么要实际引用
Button对象。听起来你只是想知道项目的项目索引,在这种情况下,我会将VoltageModel传递为CommandParameter,并使用VoltageCollection.IndexOf(ItemPassedInCommandParameter)查找当前项目的索引。 -
@StonedJesus 我正要回答你的问题,但看到有人已经在下面发布了答案:)