我想动态添加项目到tabcontrol

时间:2019-01-22 04:37:56

标签: c# mvvm tabcontrol

我正在制作带有MVVM模式的WPF应用程序。

是我多次单击一个按钮,如果单击“子按钮”,则会显示带有许多选项卡的子窗口。

标签计数与点击次数相同。

但是我想在一个选项卡中包含项目。

点击次数并不总是一样。

应该如何编辑我的代码?

//视图

<Grid>
    <Button Content="CountUp" HorizontalAlignment="Left" Height="76" Margin="72,57,0,0" VerticalAlignment="Top" Width="135" Click="OnClickCountUp"/>
    <Button Content="OpenSubWindow" HorizontalAlignment="Left" Height="74" Margin="269,202,0,0" VerticalAlignment="Top" Width="123" Click="OnClicOpenSubWindow"/>

    <Label Content="Count : " HorizontalAlignment="Left" Height="28" Margin="254,73,0,0" VerticalAlignment="Top" Width="53"/>
    <Label Content="{Binding CountVal}" HorizontalAlignment="Left" Height="28" Margin="307,73,0,0" VerticalAlignment="Top" Width="67"/>
</Grid>

// ViewModel

public class MainViewModel : INotifyPropertyChanged
{
    private string count;
    public string CountVal
    {
        get { return count; }
        set
        {
            count = value;
            NotifyPropertyChanged("CountVal");
        }

    }
    public ObservableCollection<TabItemData> TabItems { get; set; } = new ObservableCollection<TabItemData>();


    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

public class TabItemData
{
    public string Header { get; set; }
    public ObservableCollection<CheckBox> Content { get; set; }
}

//隐藏代码

public partial class MainWindow : Window
{
    private MainViewModel mViewmodel;
    int count = 0;

    public MainWindow()
    {
        InitializeComponent();
        mViewmodel = new MainViewModel();
        this.DataContext = mViewmodel;
    }

    private void OnClicOpenSubWindow(object sender, RoutedEventArgs e)
    {
        Window1 window1 = new Window1(mViewmodel);
        window1.Show();
    }

    private void OnClickCountUp(object sender, RoutedEventArgs e)
    {
        count++;

        ObservableCollection<CheckBox> items = new ObservableCollection<CheckBox>();

        for (int i = 0; i <= count; i++)
        {
            CheckBox checkBox = new CheckBox();
            checkBox.IsChecked = false;
            checkBox.Content = count.ToString();

            items.Add(checkBox);
        }

        mViewmodel.CountVal = count.ToString();
        mViewmodel.TabItems.Add(new TabItemData() { Header = count.ToString(), Content = items });
    }
}

// SubWindow的视图

<Grid>
    <TabControl ItemsSource="{Binding TabItems}">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Header}" />
            </DataTemplate>
        </TabControl.ItemTemplate>

        <TabControl.ContentTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding Content}" />
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>
</Grid>

// SubWindow的代码隐藏

public partial class Window1 : Window
{
    public Window1(MainViewModel pMainViewModel)
    {
        InitializeComponent();
        this.DataContext = pMainViewModel;
    }
}

还有补品。

@我想使用复选框作为“选项卡”中的项目。所以我不想改变。

@如果可以的话,我想每5个项目添加一个标签。如果您提供该代码,我会感到非常高兴。 (例如,page1包含item1、2、3、4和5。page2包含item6、7 ...,依此类推。)

0 个答案:

没有答案