私は C++ 開発者で、最近 C# WPF に移行しました。ボタン、ラベル、テキストボックス、トグルボタンを動的に生成することになっているWPFアプリに取り組んでいます。私は次のことをしました:
XAML:
<ListBox x:Name="myViewChannelList" HorizontalAlignment="Stretch" Height="Auto" ItemsSource="{Binding VoltageCollection}" Margin="0" VerticalAlignment="Stretch" Width="Auto">
<ListBox.ItemTemplate>
<DataTemplate >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="170" />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="{Binding ChannelName}" Margin="50,20,0,0"></Label>
<Grid Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Text="{Binding VoltageText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="25" Width="50" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="170,20,0,0" />
<Button Grid.Column="1" Content="Set" Height="25" CommandParameter="{Binding VoltageText}" Command="{Binding VoltageCommand}" Width="50" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="20,20,0,0" ></Button>
</Grid>
<Label Grid.Column="2" Content="{Binding Path=Voltage}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="210,20,0,0" ></Label>
<ToggleButton Grid.Column="3" Content="On" Height="25" Width="30" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="120,20,0,0" ></ToggleButton>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Redhook" Command="{Binding RedhookCommand}" FontSize="13" Height="25" HorizontalAlignment="Center" Margin="0,0,0,0" Name="Refresh1Btn" VerticalAlignment="Center" Width="105" />
ViewModel クラス:
class ChannelList : INotifyPropertyChanged
{
private ICommand m_voltageCommand;
public ChannelList()
{
m_voltageCommand = new DelegateVoltageCommand(x => SetCommandExecute(x));
}
public void Initialize()
{
VoltageCollection = new ObservableCollection<VoltageModel> { new VoltageModel() { ChannelName = "VDD__Main", VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
new VoltageModel() { ChannelName = "VDD__IO__AUD", VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
new VoltageModel() { ChannelName = "VDD__CODEC__AUD", VoltageText = String.Empty, VoltageCommand = m_voltageCommand }
};
}
public ObservableCollection<VoltageModel> VoltageCollection { get; set; }
/// <summary>
/// Event On Get Current Frquency Button Click
/// </summary>
private ICommand mRedhookCommand;
public ICommand RedhookCommand
{
get
{
if (mRedhookCommand == null)
mRedhookCommand = new DelegateCommand(new Action(GetCurrentFreqExecuted), new Func<bool>(GetCurrentFreqCanExecute));
return mRedhookCommand;
}
set
{
mRedhookCommand = value;
}
}
public bool GetCurrentFreqCanExecute()
{
return true;
}
public void GetCurrentFreqExecuted()
{
VoltageCollection.Clear();
VoltageCollection = new ObservableCollection<VoltageModel> { new VoltageModel() { ChannelName = "VDD__Main", VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
new VoltageModel() { ChannelName = "VDD__IO__AUD", VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
new VoltageModel() { ChannelName = "VDD__CODEC__AUD", VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
new VoltageModel() { ChannelName = "VDD__LDO", VoltageText = String.Empty, VoltageCommand = m_voltageCommand},
new VoltageModel() { ChannelName = "VDD__AMP", VoltageText = String.Empty, VoltageCommand = m_voltageCommand}
};
}
したがって、アプリケーションの起動時に、動的に生成されたコントロールを表示できます。しかし、REDHOOK
ボタンをクリックすると、リストに記載されているように新しい値でリストを更新したいと思いますGetCurrentFreqExecuted()
。これを行おうとすると、リストはクリアされますが、新しい値は更新されません。どうすればこれを達成できますか???