开发者

How to fill a listbox based on Environment.ProcessorCount

I want to fill a listbox/combobox with values like "CPU 1", "CPU 2", etc.

The number of items will be equal to Environment.ProcessorCount.

I know the approach with user controls and 开发者_JAVA百科C# programming, but i want this all in XAML. would you please show me some directions or provide some samples ?

Thx in Adv


You will have to some some form of code behind/VM to do this. You can't do all in xaml.

I would:

  1. Create a view model class
  2. Create a property on that class which is some form of IEnumerable<string>. Using an ObservableCollection<string> here might be overkill given that people don't tend to have the number of processors change at all.
  3. When the view model instance is instantiated, generate your strings (ie. for(var i = 0; i < Environment.ProcessorCount; ++i) { CpuList.Add(string.Format("CPU {0}", i + 1)); }
  4. Bind your XAML to this view model property <ListBox ItemsSource="{Binding CpuList}" /> after setting the DataSource for the list or parent window to the view model instance: someWindow.DataContext = new ViewModelClass();

Doneski!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜