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"  />

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()。これを行おうとすると、リストはクリアされますが、新しい値は更新されません。どうすればこれを達成できますか???

4

2 に答える 2

1

問題は、インスタンスが変更されたときに VoltageCollection が通知しないことです。そのため、VoltageCollection にインスタンスを割り当てるときに PropertyChanged を呼び出す必要があります。

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

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

     }

他の方法、

GetCurrentFreqExecuted()メソッドでは、VoltageCollection にインスタンスを割り当てないでください。つまり、削除VoltageCollection=new ObservableCollection<>....().

そして、 for each ループを使用して各項目を追加します。

今あなたのコードは次のようになります

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 に答える
1

こんにちは、 VoltageModel クラスに INotifyPropertyChanged を実装し、 ChannelName 、 VoltageText などの各プロパティに通知します。これが役立つことを願っています

于 2012-10-15T05:03:18.823 に答える