0

我正在 WPF 中开发一个应用程序,我应该在其中动态生成 UI 组件并对其执行一些操作。我这样做如下:

XAML:

<Grid Visibility="{Binding IsAvailable, Converter={StaticResource booltovisibility}}">            
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="170" />
        <ColumnDefinition />
        <ColumnDefinition Width="130" />
        <ColumnDefinition Width="115" />
    </Grid.ColumnDefinitions>

    <Label Grid.Column="0" Content="{Binding ChannelName}" />
    <TextBox Grid.Column="1" Text="{Binding VoltageText}" />
    <Button Grid.Column="1" Content="Set" CommandParameter="{Binding VoltageText}" Command="{Binding VoltageCommand}" />
    <Label Grid.Column="2" Content="{Binding CurrentText}"  />
    <ToggleButton Grid.Column="3" Content="On" />
</Grid>

<Button Content="Bavaria" Name="BavariaBtn" Click="BavariaBtn_Click" />

XAML.cs:

public void OnChildAdd()
    {            
        foreach (VoltageBoardChannel mVoltageChannelViewModel in mVoltageViewModel.VoltageChannelList)
        {
            VoltageChannelView mVoltageChannelView = new VoltageChannelView();
            mVoltageChannelView.Margin = new Thickness(2);
            mVoltageChannelView.ChannelInfo = mVoltageChannelViewModel;
            //Some Code to add the item
        }
    }

    private void BavariaBtn_Click(object sender, RoutedEventArgs e)
    {
        VoltageViewModel mVoltageViewModel = new VoltageViewModel();

        // Here List should get cleared and Bavaria 2 items should be added
        mVoltageViewModel.VoltageChannelList.Clear();
        mVoltageViewModel.VoltageChannelList = mVoltageViewModel.bavaria2Channels;            
        OnChildAdd();
    }      

视图模型:

public List<VoltageBoardChannel> channelList = null;        

    public List<VoltageBoardChannel> bavaria2Channels = new List<VoltageBoardChannel>
    {
         new VoltageBoardChannel { ChannelName = "VDD__MAIN", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD__IO__AUD", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD__CODEC__AUD", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD__DAL__AUD", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD__DPD__AUD", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD__PLL__AUD", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD__AMP1__AUD", IsAvailable = true},
    };

    private ICommand m_voltageCommand;        

    public List<VoltageBoardChannel> bavaria1Channels = new List<VoltageBoardChannel>
    {
         new VoltageBoardChannel { ChannelName = "VDD__MAIN", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD__IO", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD__CODEC", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD__LDO", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD__AMP", IsAvailable = true},  
    };            

    public VoltageViewModel()
    {
        channelList = new List<VoltageBoardChannel>(0);
        channelList = bavaria1Channels;            
        m_voltageCommand = new DelegateVoltageCommand(x => SetCommandExecute(x));            
    }

    public List<VoltageBoardChannel> VoltageChannelList
    {
        get 
        { 
            return channelList; 
        }

        set
        { 
            channelList = value;
            OnPropertyChanged("ChannelList");
        }
    }

型号类:

private string mChannelName;
    public string ChannelName
    {
        get
        {
            return mChannelName;
        }
        set
        {
            mChannelName = value;
            OnPropertyChanged("ChannelName");
        }
    }

    private bool mIsAvailable;
    public bool IsAvailable
    {
        get
        {
            return mIsAvailable;
        }
        set
        {
            mIsAvailable = value;
            OnPropertyChanged("IsAvailable");
        }
    }        

    public ICommand VoltageCommand { get; set; }

您可以在 ViewModel 类中注意到 Bavaria 1 在启动时显示。我在这里基本上想要的是清除项目,然后显示 Bavaria 2 项目,BavariaBtn_Click()如 xaml.cs 类所示。我正在尝试这样做,但列表没有被清除,尽管正在添加项目。

我怎样才能实现它?:)

4

3 回答 3

1

您正在清除一个局部变量。XAML 不知道你已经这样做了,事实上没有人知道你已经清除了局部变量,但方法本身:

// Nobody knows the trouble I've seen...
VoltageViewModel mVoltageViewModel = new VoltageViewModel();

// Here List should get cleared and Bavaria 2 items should be added
mVoltageViewModel.VoltageChannelList.Clear();

清除列表的 MVVM 方法是将 Clear 命令添加到您的视图模型中:

public ICommand ClearCommand { get; private set; }

// ...

public VoltageViewModel(...)
{
    // ...
    this.ClearCommand = new DelegateCommand(
        () => this.VoltageChannelList.Clear(),
        () => this.VoltageChannelList.Count > 0);
}

然后在您的 XAML 中:

<Button Content="Bavaria" x:Name="BavariaBtn" Command="{Binding ClearCommand}" />
于 2012-10-15T15:29:34.957 回答
0

首先...您确实需要使用 ObservableCollection。正如@Miklos 所说,该列表不会发送通知。

其次,当你这样做时:

mVoltageViewModel.VoltageChannelList = mVoltageViewModel.bavaria2Channels;

您正在清除 VoltageChannelList 列表。你不能做这个。您将不得不遍历列表并手动复制项目

private void BavariaBtn_Click(object sender, RoutedEventArgs e)   
    {   
        VoltageViewModel mVoltageViewModel = new VoltageViewModel();   

        // Here List should get cleared and Bavaria 2 items should be added   
        mVoltageViewModel.VoltageChannelList.Clear();   

        foreach (VoltageBoardChannel channel in  mVoltageViewModel.bavaria2Channels) 
            mVoltageViewModel.VoltageChannelList.Add(channel);
    }  
于 2012-10-15T15:36:43.610 回答
0

简单列表不会发送有关其元素更改的通知。它不实现INotifyCollectionChanged接口。

ObservableCollecition确实如此,因此请使用ObservableCollection而不是 list 作为您的VoltageChannelList属性,并且您不需要OnPropertyChanged("VoltageChannelList");在设置器中

于 2012-10-15T15:11:56.607 回答