I am a C++ developer and I recently shifted to C#. I am working on a wpf app where I have to dynamically generate button, labels and textbox. Along with dynamically generating them, I have made sure only those channels must be displayed which are available by maintaining a bool available property. Here is the code:
XAML:
<Grid Visibility="{Binding IsAvailable, Converter={StaticResource booltovisibility}}">
<Label Grid.Column="0" Content="{Binding ChannelName}" />
<TextBox Grid.Column="1" Text="{Binding VoltageText}" PreviewTextInput="VoltageBox_PreviewTextInput" />
<Button Grid.Column="1" Content="Set" CommandParameter="{Binding}" Command="{Binding VoltageCommand}" />
<Label Grid.Column="2" Content="{Binding CurrentText}" />
<ToggleButton Grid.Column="3" Content="On" Command="{Binding VoltageToggleCommand}" IsChecked="{Binding Path=IsChecked}" />
</Grid>
<Button Content="Refresh All" Grid.Column="1" Command="{Binding Path=RefreshAllButtonCommand}" Name="RefreshAllBtn" />
ViewModel:
public ObservableCollection<VoltageBoardChannel> channelList = null;
public ObservableCollection<VoltageBoardChannel> bavaria1Channels = new ObservableCollection<VoltageBoardChannel>
{
new VoltageBoardChannel { ChannelName = "VDD__MAIN", IsAvailable = true, ID = 1},
new VoltageBoardChannel { ChannelName = "VDD__IO", IsAvailable = true, ID = 2},
new VoltageBoardChannel { ChannelName = "VDD__CODEC", IsAvailable = true, ID = 3},
new VoltageBoardChannel { ChannelName = "VDD__LDO", IsAvailable = true, ID = 4},
new VoltageBoardChannel { ChannelName = "VDD__AMP", IsAvailable = true, ID = 5},
new VoltageBoardChannel { ChannelName = "", IsAvailable = false, ID = 6},
new VoltageBoardChannel { ChannelName = "", IsAvailable = false, ID = 7},
new VoltageBoardChannel { ChannelName = "", IsAvailable = false, ID = 8},
new VoltageBoardChannel { ChannelName = "", IsAvailable = false, ID = 9},
new VoltageBoardChannel { ChannelName = "", IsAvailable = false, ID = 10},
new VoltageBoardChannel { ChannelName = "", IsAvailable = false, ID = 11},
new VoltageBoardChannel { ChannelName = "", IsAvailable = false, ID = 12},
};
public VoltageViewModel()
{
channelList = new ObservableCollection<VoltageBoardChannel>();
channelList = bavaria1Channels;
}
public ObservableCollection<VoltageBoardChannel> VoltageChannelList
{
get
{
return channelList;
}
set
{
channelList = value;
OnPropertyChanged("ChannelList");
}
}
void RefreshAllClick()
{
//Here I want to check if channel are available or not. If yes then execute few statements.
}
Model Class:
private string mChannelName;
public string ChannelName
{
get
{
return mChannelName;
}
set
{
mChannelName = value;
OnPropertyChanged("ChannelName");
}
}
private bool mIsAvailable;
public bool IsAvailable
{
get
{
return mIsAvailable;
}
set
{
mIsAvailable = value;
OnPropertyChanged("IsAvailable");
}
}
double voltageText;
public double VoltageText
{
get
{
return voltageText;
}
set
{
if (value > 5.0D || value < 0.0D)
throw new InvalidOperationException();
voltageText = value;
OnPropertyChanged("VoltageText");
}
}
string currentText = "0";
public string CurrentText
{
get
{
return currentText + " V";
}
set
{
currentText = value;
OnPropertyChanged("CurrentText");
}
}
int index ;
public int ID
{
get
{
return index;
}
set
{
index = value;
OnPropertyChanged("ID");
}
}
Thus if you notice in my Bavaria1 Channel you will find few channels are available(true) and few are false. Thus on startup it displays only the Available channels. Thus using booltovisibility class I am able to set the visibility in my grid and display accordingly Now here is what I want to achieve:
When I click RefreshAll Button, a method called RefreshAll_Click() is triggered and I want to check in a if condition which channels are available. If yes then execute few statements. I had done this in C++ as follows:
if(m_voltageChannels[channel].available) // Here channel is 12 as you can notice in bavaria1 list
{
cmd = (0x8400 | (channel & 0xFF));
String OldValue = m_labelCurrentVoltage[channel]->getText();
//Some Code
}
How can I achieve this? 🙂
I would use a
foreachloop, or linq. Since you’re starting out in C#, here’s the foreach approach:Or, since
ObservableCollection<T>implementsIList<T>, you can index into the collection, as in your C++ code:If you just need a boolean that is true iff at least one channel is available, linq is best:
Moved from my previous (now deleted) comment, here’s some unsolicited further advice:
You should remove the line
channelList = new ObservableCollection<VoltageBoardChannel>();, since you’re immediately discarding that object by reassigning the reference in the next line (channelList = bavaria1Channels;). In fact, you don’t needbavaria1Channels(nor the constructor) at all; you can just declare and initializechannelListwith the collection initializer expression, like this: