使用UserControls多次使用不同的UserControl名称&数据

时间:2015-08-20 07:12:13

标签: c# wpf mvvm combobox datacontext

因此,在互联网的一点帮助下,我建立了很好的GUI内容(谢谢谷歌:))我可以在ComboBox的帮助下在一个ContentControl中的不同UserControl之间进行更改。所以因为我在WPF中很新鲜,所以我觉得你们有些问题。

当程序开始刷新所有视图的Combobox项目时。我可以多次添加相同的视图。代码“Main”窗口ViewModel:

ConfigurationDialog.xaml(主窗口)代码:

<Window.Resources>
    <DataTemplate DataType="{x:Type ViewModel:GeneralSettingsViewModel}">
        <View:GeneralSettingsView/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type ViewModel:AdvancedSettingsViewModel}">
        <View:AdvancedSettingsView/>
    </DataTemplate>
</Window.Resources>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition Width="3*"/>
    </Grid.ColumnDefinitions>

    <ComboBox x:Name="ComboBoxMenu" Grid.Column="0" Margin="5" Height="20" ItemsSource="{Binding Settings}" SelectedIndex="0">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" Padding="10"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

    <Border Grid.Column="1" Margin="5" BorderBrush="#FF7F9DB9" BorderThickness="1">
        <ContentControl Content="{Binding ElementName=ComboBoxMenu, Path=SelectedItem}"/>
    </Border>

</Grid>

ConfigurationDialogViewModel中的代码:

public class ConfigurationDialogViewModel : ViewModelBase
{
    private readonly ObservableCollection<SettingsViewModelBase> settings;

    public ObservableCollection<SettingsViewModelBase> Settings
    {
        get { return this.settings; }
    }

    public ConfigurationDialogViewModel()
    {
        this.settings = new ObservableCollection<SettingsViewModelBase>();



        this.settings.Add(new GeneralSettingsViewModel());
        this.settings.Add(new AdvancedSettingsViewModel());
        this.settings.Add(new GeneralSettingsViewModel());
    }
}

所以我建立了两次“GeneralSettingsViewModel()”,它就像那样工作得很好。只有我不喜欢的东西总是同名。在我的情况下,它显示两次“常规设置”名称。我喜欢“常规设置1”和“常规设置2”等.GeneralSettingsViewModel代码:

public class GeneralSettingsViewModel : SettingsViewModelBase
{
    public override string Name
    {
        get { return "General"; }
    }
}

public abstract class SettingsViewModelBase : ViewModelBase
{
    public abstract string Name { get; }
}

稍后当选择“常规设置1”和数据inputet时,我喜欢它不是更多的“常规设置1”,所以如何从ComboBox中删除该项目。

那么可以在“主窗口”ViewModel(在我的情况下为“ConfigurationDialogViewModel”)中手动生成组合框中项目的名称以及以后如何删除它?我之间也必须捕获UserControl的inputet日期。

查看其中一个UserControls:

<UserControl x:Class="ConfigurationDialogExample.View.GeneralSettingsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
    <TextBlock Text="General settings" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>

如有任何问题请询问。对不起,如果询问“错误”的问题(我是WPF新手)。

1 个答案:

答案 0 :(得分:0)

行。我想解决它:

MainWindow ViewModel代码:

public ConfigurationDialogViewModel()
    {
        this.settings = new ObservableCollection<SettingsViewModelBase>();

        GeneralSettingsViewModel GeneralSettings1 = new GeneralSettingsViewModel();
        GeneralSettingsViewModel GeneralSettings2 = new GeneralSettingsViewModel();
        GeneralSettingsViewModel GeneralSettings3 = new GeneralSettingsViewModel();
        AdvancedSettingsViewModel AdvancedSettingsViewModel1 = new AdvancedSettingsViewModel();
        AdvancedSettingsViewModel AdvancedSettingsViewModel2 = new AdvancedSettingsViewModel();
        AdvancedSettingsViewModel AdvancedSettingsViewModel3 = new AdvancedSettingsViewModel();

        GeneralSettings1.Name = "keks1";
        GeneralSettings2.Name = "keks2";
        GeneralSettings3.Name = "keks3";

        AdvancedSettingsViewModel1.Name = "banana1";
        AdvancedSettingsViewModel2.Name = "banana2";
        AdvancedSettingsViewModel3.Name = "banana3";

        this.settings.Add(GeneralSettings1);
        this.settings.Add(GeneralSettings2);
        this.settings.Add(GeneralSettings3);
        this.settings.Add(AdvancedSettingsViewModel1);
        this.settings.Add(AdvancedSettingsViewModel2);
        this.settings.Add(AdvancedSettingsViewModel3);



    }

MainWindow查看代码:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition Width="3*"/>
    </Grid.ColumnDefinitions>

    <ComboBox x:Name="ComboBoxMenu" Grid.Column="0" Margin="5" Height="20" ItemsSource="{Binding Settings}" SelectedIndex="0">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" Padding="10"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

    <Border Grid.Column="1" Margin="5" BorderBrush="#FF7F9DB9" BorderThickness="1">
        <ContentControl Content="{Binding ElementName=ComboBoxMenu, Path=SelectedItem}"/>
    </Border>

</Grid>

UserControl代码之一:

public class AdvancedSettingsViewModel : SettingsViewModelBase
{
    public override string Name
    {
        get; set;
    }


}