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

本文关键字:UserControl 名称 数据 | 更新日期: 2023-09-27 18:08:47

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

当程序启动它的刷新组合框项目与所有的视图。我可以多次添加相同的视图。"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"answers"常规设置2"等。GeneralSettingsViewModel代码:

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

以后当例如"通用设置1"被选择和数据输入时,我喜欢它不是更多的"通用设置1"可用,所以如何从组合框中删除该项。

所以有可能在"主窗口"ViewModel(在我的情况下"ConfigurationDialogViewModel")中手动生成组合框中的项目名称,以及以后如何删除它?此外,我必须捕获UserControl的输入日期。

查看其中一个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)。

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

OK。我想我解决了它:

MainWindow ViewModel Code:

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);

    }

主窗口视图代码:

<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 Code:

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

}