0

我是 WPF C# 世界的新手。我的构造函数中有一个方法,可以将子项添加到堆栈面板。我有 2 个 xaml 文件。mainone 有一个堆栈面板,另一个有一个标签。

主窗口视图:

<Grid Grid.Row="1" Name="VoltageChannels" >
        <StackPanel Height="Auto" Name="stackPanel" Width="Auto" MinHeight="300"> </StackPanel>
    </Grid>

<Button Content="Refresh All" Command="{Binding AddChildCommand}"  Name="RefreshAllBtn" />


public void OnChildAdd()
    {
        foreach (VoltageBoardChannel mVoltageChannelViewModel in mVoltageViewModel.VoltageChannelList)
        {
            VoltageChannelView mVoltageChannelView = new VoltageChannelView();
            mVoltageChannelView.Margin = new Thickness(2);
            mVoltageChannelView.ChannelInfo = mVoltageChannelViewModel;
            stackPanel.Children.Add(mVoltageChannelView);
        }
    }

我想从我的视图模型类中访问此方法,以通过单击按钮添加子项。基本上我有一个包含一组项目的列表。这些项目应在按钮单击时显示:) 这是 View 和 ViewModel 类:

视图模型:

public viewModel()
{

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

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

List<VoltageBoardChannel> channelList = new List<VoltageBoardChannel>(0);

    // VoltageBoardChannel has Channel name and avalable as property.
    List<VoltageBoardChannel> redhookChannels = new List<VoltageBoardChannel>
    {
         new VoltageBoardChannel { ChannelName = "", IsAvailable = false},
         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},             
    };

    private ICommand mRefreshAllCommand;
    public ICommand AddChildCommand
    {
        get
        {
            if (mRefreshAllCommand == null)
                mRefreshAllCommand = new DelegateCommand(new Action(mRefreshAllCommandExecuted), new Func<bool>(mRefreshAllCommandCanExecute));

            return mRefreshAllCommand;
        }
        set
        {
            mRefreshAllCommand = value;
        }
    }

    public bool mRefreshAllCommandCanExecute()
    {
        return true;
    }

    public void mRefreshAllCommandExecuted()
    {                               
        VoltageChannelList = bavaria1Channels;          
        // Call OnChildAdd Method here                      
    }

具有我的标签的 XAML 视图:

<Label Grid.Column="0" Content="{Binding ChannelName}" Height="25" Width="120" Name="VoltageLabel" />

电压板通道类:

public class VoltageBoardChannel
{
    public string ChannelName { get; set; }
    public bool IsAvailable { get; set; }
}     

在按钮单击时调用的方法。单击后我想调用OnChildAdd()方法以将这些项目列表添加LIST到我的堆栈面板中。可能吗???

4

1 回答 1

1

在xml中:

<Button Command={Binding AddChildCommand} />

在您的视图模型中:

public ICommand AddChildCommand { get; private set; }

然后在 viewmodel 的构造函数中,设置AddChildCommand为实现ICommand. 然后,此命令可以调用您的OnChildAdd()方法。(是规格)

但是,更好的方法是将stackpanel的 Children 绑定到视图模型中的属性,然后将VoltageChannelViews 添加到该属性。

于 2012-10-12T13:18:08.637 回答