我正在 WPF 中开发一个应用程序,我应该在其中动态生成 UI 组件并对其执行一些操作。我这样做如下:
XAML:
<Grid Visibility="{Binding IsAvailable, Converter={StaticResource booltovisibility}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="170" />
<ColumnDefinition />
<ColumnDefinition Width="130" />
<ColumnDefinition Width="115" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="{Binding ChannelName}" />
<TextBox Grid.Column="1" Text="{Binding VoltageText}" />
<Button Grid.Column="1" Content="Set" CommandParameter="{Binding VoltageText}" Command="{Binding VoltageCommand}" />
<Label Grid.Column="2" Content="{Binding CurrentText}" />
<ToggleButton Grid.Column="3" Content="On" />
</Grid>
<Button Content="Bavaria" Name="BavariaBtn" Click="BavariaBtn_Click" />
XAML.cs:
public void OnChildAdd()
{
foreach (VoltageBoardChannel mVoltageChannelViewModel in mVoltageViewModel.VoltageChannelList)
{
VoltageChannelView mVoltageChannelView = new VoltageChannelView();
mVoltageChannelView.Margin = new Thickness(2);
mVoltageChannelView.ChannelInfo = mVoltageChannelViewModel;
//Some Code to add the item
}
}
private void BavariaBtn_Click(object sender, RoutedEventArgs e)
{
VoltageViewModel mVoltageViewModel = new VoltageViewModel();
// Here List should get cleared and Bavaria 2 items should be added
mVoltageViewModel.VoltageChannelList.Clear();
mVoltageViewModel.VoltageChannelList = mVoltageViewModel.bavaria2Channels;
OnChildAdd();
}
视图模型:
public List<VoltageBoardChannel> channelList = null;
public List<VoltageBoardChannel> bavaria2Channels = new List<VoltageBoardChannel>
{
new VoltageBoardChannel { ChannelName = "VDD__MAIN", IsAvailable = true},
new VoltageBoardChannel { ChannelName = "VDD__IO__AUD", IsAvailable = true},
new VoltageBoardChannel { ChannelName = "VDD__CODEC__AUD", IsAvailable = true},
new VoltageBoardChannel { ChannelName = "VDD__DAL__AUD", IsAvailable = true},
new VoltageBoardChannel { ChannelName = "VDD__DPD__AUD", IsAvailable = true},
new VoltageBoardChannel { ChannelName = "VDD__PLL__AUD", IsAvailable = true},
new VoltageBoardChannel { ChannelName = "VDD__AMP1__AUD", IsAvailable = true},
};
private ICommand m_voltageCommand;
public List<VoltageBoardChannel> bavaria1Channels = new List<VoltageBoardChannel>
{
new VoltageBoardChannel { ChannelName = "VDD__MAIN", IsAvailable = true},
new VoltageBoardChannel { ChannelName = "VDD__IO", IsAvailable = true},
new VoltageBoardChannel { ChannelName = "VDD__CODEC", IsAvailable = true},
new VoltageBoardChannel { ChannelName = "VDD__LDO", IsAvailable = true},
new VoltageBoardChannel { ChannelName = "VDD__AMP", IsAvailable = true},
};
public VoltageViewModel()
{
channelList = new List<VoltageBoardChannel>(0);
channelList = bavaria1Channels;
m_voltageCommand = new DelegateVoltageCommand(x => SetCommandExecute(x));
}
public List<VoltageBoardChannel> VoltageChannelList
{
get
{
return channelList;
}
set
{
channelList = value;
OnPropertyChanged("ChannelList");
}
}
型号类:
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");
}
}
public ICommand VoltageCommand { get; set; }
您可以在 ViewModel 类中注意到 Bavaria 1 在启动时显示。我在这里基本上想要的是清除项目,然后显示 Bavaria 2 项目,BavariaBtn_Click()
如 xaml.cs 类所示。我正在尝试这样做,但列表没有被清除,尽管正在添加项目。
我怎样才能实现它?:)