1

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

4

2 回答 2

1

嗨,在 VoltageModel 类中实现 INotifyPropertyChanged 并通知 ChannelName、VoltageText 等每个属性。我希望这个能帮上忙

于 2012-10-15T05:03:18.823 回答
1

问题是,当实例更改时,您的 VoltageCollection 不会通知。因此,您需要在将实例分配给 VoltageCollection 时调用 PropertyChanged。

    ObservableCollection<VoltageModel> _voltages;
    public ObservableCollection<VoltageModel> VoltageCollection {

     get {return voltages;}
     set 
     {
     _volgates=value;
      PropertyChanged("VoltageCollection");

     }

另一种方式,

在您的GetCurrentFreqExecuted()方法中,不要将实例分配给 VoltageCollection。即,删除VoltageCollection=new ObservableCollection<>....().

并为每个循环添加每个项目。

现在你的代码看起来像

VoltageCollection.Clear();
    VoltageModel[] models = { new VoltageModel() { ChannelName = "VDD_Main", VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
                              new VoltageModel() { ChannelName = "VDD_Main", VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
                              new VoltageModel() { ChannelName = "VDD_Main", VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
                              new VoltageModel() { ChannelName = "VDD_Main", VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
                              new VoltageModel() { ChannelName = "VDD_Main", VoltageText = String.Empty, VoltageCommand = m_voltageCommand }
                            };

    foreach (var model in models)
    {
        VoltageCollection.Add(model);
    }
于 2012-10-15T05:08:14.547 回答