我很抱歉标题很奇怪。我正在处理 UI 组件的动态生成。我的 Xaml 类中有一个 ListBox 动态生成按钮。
看法:
<ListBox x:Name="SetButtonList" ItemsSource="{Binding}" >
<ListBox.ItemTemplate>
<DataTemplate >
<Grid>
<Button Content="{Binding Path=SetButtonFreq}" Command="{Binding Path=SetFreqCommand}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
视图模型:
private ICommand mSetFreqCommand;
public ICommand SetFreqCommand
{
get
{
if (mSetFreqCommand == null)
mSetFreqCommand = new DelegateCommand(new Action(SetFreqCommandExecuted), new Func<bool>(SetFreqCommandCanExecute));
return mSetFreqCommand;
}
set;
}
public bool SetFreqCommandCanExecute()
{
return true;
}
public void SetFreqCommandExecuted()
{
// Executes the Body when Button is Clicked
}
模型:
private string _SetFreqValue = "";
public String SetButtonFreq
{
set; get;
}
public ClockModel(string SetButtonFreq)
{
this.SetButtonFreq = SetButtonFreq;
}
public override string ToString()
{
return _SetFreqValue;
}
查看.cs 文件:
// mClock is object of Model Class
mClock.Add(new ClockModel("Set 12.0 MHz"));
mClock.Add(new ClockModel("Set 12.288 MHz"));
mClock.Add(new ClockModel("Set 15.36 MHz"));
mClock.Add(new ClockModel("Set 19.2 MHz"));
这在我的列表框中为我提供了 4 个不同频率的按钮。但是当我尝试单击它们时,控件不会进入该SetFreqCommandExecuted()
方法。我在哪里犯错?:(
请帮忙