我正在开发一个 WPF,我需要在其中动态生成 16 次 Togglebutton。如果我写下这个切换按钮 16 次并为它们设置单独的按钮单击命令,那将是低效的。
XAML:
<Togglebutton Height="14" Command="{Binding TogglebuttonGen}" Margin="0" Name="checkBox1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center" />
理想情况下,我希望生成它们 16 次,并在我的 viewmodel 类中使用一种通用方法,如下所示:
private ICommand mTogglebuttonGen;
public ICommand TogglebuttonGen
{
get
{
if (mTogglebuttonGen == null)
mTogglebuttonGen = new DelegateCommand(new Action(mTogglebuttonGenExecuted), new Func<bool>(mTogglebuttonGenCanExecute));
return mTogglebuttonGen;
}
set
{
mTogglebuttonGen = value;
}
}
public bool mTogglebuttonGenCanExecute()
{
return true;
}
public void mTogglebuttonGenExecuted(some INDEX parameter which gives me selected Togglebutton)
{
// Have a common method here which performs operation on each Togglebutton click based on INDEX which determines which Togglebutton I have selected
}
我在我的 C++ 应用程序中完成了如下操作:
for(int j = 0; j < 16; j ++)
{
m_buttonActiveChannels[j] = new ToggleButton();
addAndMakeVisible(m_buttonActiveChannels[j]);
m_buttonActiveChannels[j]->addButtonListener(this);
}
//Checking which Togglebutton is clicked
unsigned bit = 0x8000;
for(int i = 15; i >= 0; i--)
{
if(0 != (value & bit)) //Value has some hardcoded data
{
m_buttonActiveChannels[i]->setToggleState(true);
}
else
{
m_buttonActiveChannels[i]->setToggleState(false);
}
bit >>= 1;
}
因此,这会生成 16 次,并且有一种基于index i
.
如何在我的 wpf 应用程序中实现它?:)