我是C ++开发人员,最近我转到了C#。我正在开发一个wpf应用程序,我必须动态生成按钮,标签和文本框。除了动态生成它们之外,我还确保只有通过维护bool可用属性才能显示那些可用的通道。这是代码:
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" />
视图模型:
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.
}
模特课程:
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");
}
}
因此,如果您在Bavaria1 Channel
中注意到,您会发现很少有频道可用(真实)而且很少有错误。因此,在启动时,它仅显示可用频道。因此,使用booltovisibility类我可以在我的网格中设置可见性并相应地显示现在这是我想要实现的:
当我点击RefreshAll
按钮时,会触发一个名为RefreshAll_Click()
的方法,我想在if
条件下检查哪些频道可用。如果是,则执行少量语句。我在C ++中完成了以下操作:
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
}
我怎样才能做到这一点? :)
答案 0 :(得分:1)
我会使用foreach
循环或linq。由于你是从C#开始的,所以这是foreach方法:
void RefreshAllClick()
{
foreach (var channel in channelList)
if (channel.IsAvailable)
//some code here
}
或者,由于ObservableCollection<T>
实现了IList<T>
,您可以索引到集合中,就像在C ++代码中一样:
if (channelList[channel].IsAvailable)
//some code here
如果你只需要一个如果至少有一个通道可用的布尔值,那么linq是最好的:
var anyChannelIsAvailable = channelList.Any(c => c.IsAvailable);
从我之前的(现已删除的)评论中移出,这是一些未经请求的进一步建议:
您应该删除行channelList = new ObservableCollection<VoltageBoardChannel>();
,因为您通过在下一行(channelList = bavaria1Channels;
)中重新分配引用来立即丢弃该对象。实际上,您根本不需要bavaria1Channels
(也不需要构造函数);您可以使用集合初始化表达式声明和初始化channelList
,如下所示:
public ObservableCollection<VoltageBoardChannel> channelList = 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},
};
答案 1 :(得分:1)
if (bavaria1Channels.Any(c => c.IsAvailable)) //Any channel available
{
//Execute some code :)
}