我是一名 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" />
视图模型类:
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()
. 当我尝试这样做时,列表会被清除但不会更新新值。我怎么能做到这一点???