无法在按钮上更新列表单击WPF

时间:2012-10-15 04:47:56

标签: c# wpf dynamic mvvm listbox

我是C ++开发人员,最近我转到了C#WPF。我正在开发一个WPF应用程序,我应该动态生成按钮,标签,文本框和togglebutton。我做了以下事情:

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()中提到的新值更新列表。当我尝试这样做时,列表被清除但不更新新值。我怎么能实现这个???

2 个答案:

答案 0 :(得分:1)

嗨,为了在VoltageModel类中实现INotifyPropertyChanged并通知每个属性,如ChannelName,VoltageText等。我希望这会有所帮助

答案 1 :(得分: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);
    }