私は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();
}
ViewModel:
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クラスで、起動時にBavaria1が表示されることがわかります。ここで基本的に必要なのは、アイテムをクリアしてからBavariaBtn_Click()
、xaml.csクラスに示すようにBavaria2アイテムを表示することです。やろうとしていますが、アイテムが追加されているのにリストがクリアされません。
どうすればそれを達成できますか?:)