在ViewModel

时间:2017-10-18 13:40:29

标签: c# wpf mvvm tabcontrol

我正在尝试在MVVM中创建一个程序,它可以读取Excel文件并将内容生成为Word文件。现在,每当有人选择在复选框上创建Word文件时,我就会坚持在Tab上创建新的UserControl

MainWindow with Checkbox

它应该在我的第二个UserControl上生成一个新标签,我试图从名为HauptfensterViewModel的ViewModel生成该标签。

Second Window where the Tabs should be generated

视图模型中的代码如下所示:

protected void OnWeiterExecute(object obj)
{
    OnDokumentChanged();
    new VorschauViewModel(view);
    if (Dokumenttyp[0] == true)
    {
        //TabItem auf VorschauWindow erstellen, mit jeweiligem Dokumentennamen(Angebotsvorlage)
    }
    if (Dokumenttyp[1] == true)
    {
        //TabItem auf VorschauWindow erstellen mit jeweiligem Dokumentennamen(Auditplan)
    }
    if (Dokumenttyp[2] == true)
    {
        //TabItem auf VorschauWindow erstellen mit jeweiligem Dokumentennamen(Checkliste)
    }
    if (Dokumenttyp[3] == true)
    {
        //TabItem auf VorschauWindow erstellen mit jeweiligem Dokumentennamen(Dokumentationsvorlage)
    }               
}

感谢您的帮助,如果有什么不可理解的话,我会尝试回答所有问题。

1 个答案:

答案 0 :(得分:0)

您是否可以尝试以下示例代码,这不是最佳解决方案,但它可以帮助您了解有关MVVM的更多信息。

视图模型:

public class HauptfensterViewModel : INotifyPropertyChanged
{
    public static HauptfensterViewModel Instance { get; } = new HauptfensterViewModel();

    private HauptfensterViewModel()
    {
    }

    private bool _property1;
    private bool _property2;
    private bool _property3;
    private bool _property4;

    public bool Property1
    {
        get { return _property1; }
        set
        {
            _property1 = value;
            OnPropertyChanged();
        }
    }

    public bool Property2
    {
        get { return _property2; }
        set
        {
            _property2 = value;
            OnPropertyChanged();
        }
    }

    public bool Property3
    {
        get { return _property3; }
        set
        {
            _property3 = value;
            OnPropertyChanged();
        }
    }

    public bool Property4
    {
        get { return _property4; }
        set
        {
            _property4 = value; 
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

的UserControl1:

<CheckBox IsChecked="{Binding Property1}"/>
<CheckBox IsChecked="{Binding Property2}"/>
<CheckBox IsChecked="{Binding Property3}"/>
<CheckBox IsChecked="{Binding Property4}"/>

UserControl2:

<TabControl HorizontalAlignment="Right">
    <TabControl.Resources>
        <BooleanToVisibilityConverter x:Key="ToVisibilityConverter"/>
    </TabControl.Resources>
    <TabItem Header="1" Visibility="{Binding Property1,Converter={StaticResource ToVisibilityConverter}}"/>
    <TabItem Header="2" Visibility="{Binding Property2,Converter={StaticResource ToVisibilityConverter}}"/>
    <TabItem Header="3" Visibility="{Binding Property3,Converter={StaticResource ToVisibilityConverter}}"/>
    <TabItem Header="4" Visibility="{Binding Property4,Converter={StaticResource ToVisibilityConverter}}"/>
</TabControl>

代码隐藏:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        DataContext = HauptfensterViewModel.Instance;
    }
}
public partial class UserControl2 : UserControl
{
    public UserControl2()
    {
        DataContext = HauptfensterViewModel.Instance;
    }
}