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:
- Create a view model class
- Create a property on that class which is some form of
IEnumerable<string>
. Using anObservableCollection<string>
here might be overkill given that people don't tend to have the number of processors change at all. - 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)); }
- 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!
精彩评论