gpt4 book ai didi

c# - 无法检索 WPF 中动态生成的按钮的内容

转载 作者:行者123 更新时间:2023-11-28 03:30:00 26 4
gpt4 key购买 nike

我正在开发 WPF 应用程序。我基本上是一名 C++ 开发人员,最近转向了 C#。在我的应用程序中,我动态生成了一组执行特定操作的按钮。

每个按钮都有一个频率,它被捕获并用于执行某些操作。在我的 C++ 应用程序中,我创建如下:

static const char *freqs[MAX_CLOCK_RANGE] = 
{
"12.0.",12.288","15.36","19.2","23.0","24.0","26.0" //MAX_CLOCK_RANGE is 7
};

for(int i = 0; i < MAX_CLOCK_RANGE; i++)
{
buttonText = String(T("Set ")) + String(freqs[i])+ String(T(" MHz"));
m_buttonSetFreq[i] = new TextButton(buttonText, String::empty);
m_buttonSetFreq[i]->addButtonListener(this);
addAndMakeVisible(m_buttonSetFreq[i]);
}

int cnt = 0;
while(cnt < MAX_CLOCK_RANGE)
{
if(button == m_buttonSetFreq[cnt])
break;
cnt++;
}
if(cnt < MAX_CLOCK_RANGE)
{
unsigned int val = String(freqs[cnt]).getDoubleValue() * 1000.0;
}
sendBuf[numBytes++] = 0x00; //SendBuf is unsigned char
sendBuf[numBytes++] = 0x00;
sendBuf[numBytes++] = (val >> 8) & 0xFF;
sendBuf[numBytes++] = val & 0xFF;

因此它从 char 数组中获取 freq 的值并对其执行上面给出的操作。 Cnt 具有单击哪个特定按钮的值,并采用单击按钮的频率。我在我的 WPF 应用程序中按如下方式进行了操作:

XAML:

<ListBox x:Name="SetButtonList" ItemsSource="{Binding}" >
<ListBox.ItemTemplate>
<DataTemplate >
<Grid>
<Button Name="FreqButton" Content="{Binding Path=SetButtonFreq}" Command="{Binding ElementName=SetButtonList, Path=DataContext.SetFreqCommand}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Xaml.cs:

ClockViewModel mClock = new ClockViewModel();

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"));
mClock.Add(new ClockModel("Set 23.0 MHz"));
mClock.Add(new ClockModel("Set 24.0 MHz"));
mClock.Add(new ClockModel("Set 26.0 MHz"));
SetButtonList.DataContext = mClock;

View 模型:

ClockModel mCModel = new ClockModel();

private ICommand mSetFreqCommand;
public ICommand SetFreqCommand
{
get
{
if (mSetFreqCommand == null)
mSetFreqCommand = new DelegateCommand(new Action(SetFreqCommandExecuted), new Func<bool>(SetFreqCommandCanExecute));

return mSetFreqCommand;
}
set
{
mSetFreqCommand = value;
}
}

public bool SetFreqCommandCanExecute()
{
return true;
}

public void SetFreqCommandExecuted()
{
//How can I retrieve the Frequency of button clicked and perform same operation as done in C++
}

型号:

public String SetButtonFreq {get; set;}

是否可以获取每个按钮的点击频率并执行与 C++ 代码中相同的步骤???请帮助:)

最佳答案

两种可能的解决方案。

第 1 步:绑定(bind)到每个按钮的 ViewModel 实例中的命令:

<Window x:Class="WpfTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox ItemsSource="{Binding Clocks}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Frequency}"
Command="{Binding SetFrequency}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>

具有以下 View 模型和代码:

class ClockViewModel  // DataContext of ListBoxItem
{
public double Frequency { get; set; }
public ICommand SetFrequency
{
get
{
return new DelegateCommand((obj) => {
double freq = this.Frequency;
// ...
});
}
}
}

class WindowViewModel
{
ObservableCollection<ClockViewModel> clocks_ =
new ObservableCollection<ClockViewModel>();
public ObservableCollection<ClockViewModel> Clocks {get {return clocks_;}}
}

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

var viewModel = new WindowViewModel();
viewModel.Clocks.Add(new ClockViewModel() { Frequency = 10.123 });
viewModel.Clocks.Add(new ClockViewModel() { Frequency = 42.0 });
viewModel.Clocks.Add(new ClockViewModel() { Frequency = 3.14 });

DataContext = viewModel;
}
}

2nd):如果命令应该是 Windows View 模型的一部分,您可以使用 CommandParameter(已更新以显示“Set 3.13 Mhz”格式):

<Window x:Class="WpfTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox Name="list" ItemsSource="{Binding Clocks}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Command="{Binding Path=DataContext.SetFrequency,
ElementName=list}"
CommandParameter="{Binding}">
<Button.Content>
<TextBlock>
<Run>Set </Run>
<Run Text="{Binding Frequency}"/>
<Run> Mhz</Run>
</TextBlock>
</Button.Content>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>

这会将按钮数据上下文传递给命令:

class ClockViewModel
{
public double Frequency { get; set; }
}

class WindowViewModel
{
ObservableCollection<ClockViewModel> clocks_ = new ObservableCollection<ClockViewModel>();
public ObservableCollection<ClockViewModel> Clocks
{
get { return clocks_; }
}

public ICommand SetFrequency
{
get{
return new DelegateCommand((obj) =>
{
var clock = obj as ClockViewModel;
DoSendBufStuffWithFrequency(clock.Frequency);
});}
}

private void DoSendBufStuffWithFrequency(double frequency)
{
}
}

关于c# - 无法检索 WPF 中动态生成的按钮的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12871733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com