Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF将多个UserControl绑定到列表框_C#_Wpf_Inheritance_User Controls_Listbox - Fatal编程技术网

C# WPF将多个UserControl绑定到列表框

C# WPF将多个UserControl绑定到列表框,c#,wpf,inheritance,user-controls,listbox,C#,Wpf,Inheritance,User Controls,Listbox,我有多个用户控件,我想在列表框中显示它们 假设我有一个Employee abstract UserControl,我有2个或更多的UserControl,而不是使用类似于AdministrativeEmployee和FactoryEmployee的Employee。对于每个员工,我有不同的数据,但UserControl非常相似(大小相同,字段几乎相同),在ViewModel端,我有一个abstract EmployeeViewModel,AdministrativeEmployeeViewMod

我有多个用户控件,我想在列表框中显示它们 假设我有一个Employee abstract UserControl,我有2个或更多的UserControl,而不是使用类似于AdministrativeEmployee和FactoryEmployee的Employee。对于每个员工,我有不同的数据,但UserControl非常相似(大小相同,字段几乎相同),在ViewModel端,我有一个abstract EmployeeViewModel,AdministrativeEmployeeViewModel和FatoryEmployeeViewModel,在EmployeesView上有一个带有数据绑定的列表框,在EmployeesView模型上有一个ICollection Employees

EmployeesView.xaml:

xmlns:local="clr-namespace:Solution.Controls"
-


EmployeesViewModel.cs:

public ICollection<EmployeeViewModel> Employees { get; set; }
public ICollection雇员{get;set;}

但是,在列表框上显示System.ItemTemplate,而不是在
列表框
资源中为每种类型的员工定义的用户控件定义了两个数据模板:

<ListBox ItemsSource="{Binding Employees}">
    <ListBox.Resources>
        <DataTemplate DataType="{x:Type local:AdministrativeEmployee}">
            <local:AdministrativeEmployeeView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:FactoryEmployee}">
            <local:FactoryEmployeeView />
        </DataTemplate>
    </ListBox.Resources>            
</ListBox>
样本数据:

List<Employee> _source = new List<Employee>();
_source.Add(new AdministrativeEmployee { Name = "A test1" });
_source.Add(new FactoryEmployee { Name = "F test1" });
_source.Add(new AdministrativeEmployee { Name = "A test2" });
_source.Add(new FactoryEmployee { Name = "F test2" });
Employees = _source;
List_source=newlist();
_Add(新的管理员雇员{Name=“A test1”});
_添加(新FactoryEmployee{Name=“F test1”});
_Add(新的管理员雇员{Name=“A test2”});
_添加(新FactoryEmployee{Name=“F test2”});
雇员=_来源;
管理员工视图:

<UserControl ...>
    <Grid>
        <TextBlock Text="{Binding Name}" Background="Red" />
    </Grid>
</UserControl>
<UserControl ...>
    <Grid>
        <TextBlock Text="{Binding Name}" Background="Green" />
    </Grid>
</UserControl>

FactoryEmployeeView:

<UserControl ...>
    <Grid>
        <TextBlock Text="{Binding Name}" Background="Red" />
    </Grid>
</UserControl>
<UserControl ...>
    <Grid>
        <TextBlock Text="{Binding Name}" Background="Green" />
    </Grid>
</UserControl>

结果:


列表框中
资源定义了两个数据模板:

<ListBox ItemsSource="{Binding Employees}">
    <ListBox.Resources>
        <DataTemplate DataType="{x:Type local:AdministrativeEmployee}">
            <local:AdministrativeEmployeeView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:FactoryEmployee}">
            <local:FactoryEmployeeView />
        </DataTemplate>
    </ListBox.Resources>            
</ListBox>
样本数据:

List<Employee> _source = new List<Employee>();
_source.Add(new AdministrativeEmployee { Name = "A test1" });
_source.Add(new FactoryEmployee { Name = "F test1" });
_source.Add(new AdministrativeEmployee { Name = "A test2" });
_source.Add(new FactoryEmployee { Name = "F test2" });
Employees = _source;
List_source=newlist();
_Add(新的管理员雇员{Name=“A test1”});
_添加(新FactoryEmployee{Name=“F test1”});
_Add(新的管理员雇员{Name=“A test2”});
_添加(新FactoryEmployee{Name=“F test2”});
雇员=_来源;
管理员工视图:

<UserControl ...>
    <Grid>
        <TextBlock Text="{Binding Name}" Background="Red" />
    </Grid>
</UserControl>
<UserControl ...>
    <Grid>
        <TextBlock Text="{Binding Name}" Background="Green" />
    </Grid>
</UserControl>

FactoryEmployeeView:

<UserControl ...>
    <Grid>
        <TextBlock Text="{Binding Name}" Background="Red" />
    </Grid>
</UserControl>
<UserControl ...>
    <Grid>
        <TextBlock Text="{Binding Name}" Background="Green" />
    </Grid>
</UserControl>

结果:


这是打字错误吗
ItemsSource={Binding Employees}
应该是
ItemsSource=“{Binding Employees}”
。而没有绑定的DataTrigger是无用的。看起来您在这里使用的方法非常迂回。为什么不定义指定类型的数据模板?这将自动呈现每个类型及其指定的模板。这是打字错误吗
ItemsSource={Binding Employees}
应该是
ItemsSource=“{Binding Employees}”
。而没有绑定的DataTrigger是无用的。看起来您在这里使用的方法非常迂回。为什么不定义指定类型的数据模板?这将自动呈现每个类型及其指定的模板。它非常接近我需要的内容,无论如何,我必须更改几行
xlmns:viewModels=“clr namespace:Solution.viewModels”
,并将数据模板更改为:
无论如何,它非常接近我需要的内容,我必须更改几行
xlmns:viewModels=“clr namespace:Solution.viewModels”
并将数据模板更改为: