C# Usercontrol作为带有绑定的datatemplate

C# Usercontrol作为带有绑定的datatemplate,c#,wpf,mvvm,user-controls,datatemplate,C#,Wpf,Mvvm,User Controls,Datatemplate,我有一个ItemsControl,其中ItemsSource绑定到系统模型列表。它必须为列表中的每个系统生成一个usercontrol。在这些用户控件中,它有一些文本框,显示系统的名称、is和位置 我的代码创建usercontrols,但不填充usercontrol中的文本框 视图: 不起作用的内容:partID=“{Binding ID}”、company=“{Binding ItemsSource.company}”、typeSorter=“{Binding ItemsSource.Name

我有一个ItemsControl,其中ItemsSource绑定到系统模型列表。它必须为列表中的每个系统生成一个usercontrol。在这些用户控件中,它有一些文本框,显示系统的名称、is和位置

我的代码创建usercontrols,但不填充usercontrol中的文本框

视图:

不起作用的内容:partID=“{Binding ID}”、company=“{Binding ItemsSource.company}”、typeSorter=“{Binding ItemsSource.Name,ElementName=SystemList}”、typeLocation=“{Binding ItemsSource.Location,ElementName=SystemList}”和buttonCommandParameter=“{Binding ItemsSource.ID,ElementName=SystemList}”

但是,如果我只使用一个按钮作为datatemplate,并使用Content=“{Binding ID}”,它就可以完美地工作,如果我在datatemplate之外使用usercontrol,它也可以工作。但它在datatemplate中不起作用

我得到的错误是:“BindingExpression路径错误:'在'object''MultiLineButton'(Name='')上找不到Company'属性。BindingExpression:path=Company;DataItem='MultiLineButton'(Name='');目标元素是'MultiLineButton'(Name='');目标属性是'Company'(类型'String')”


如何修复这些绑定?

尝试ObservableCollection:

private ObservableCollection<SystemModel> _systems = new ObservableCollection<SystemModel>();
public ObservableCollection<SystemModel> Systems { get { return _systems; } }

public SystemListViewModel()
{
    var systems = SystemAPI.Instance.GetSystems();
    foreach (var system in systems)
    {
        Systems.Add(system);
    }
}
private ObservableCollection\u systems=new ObservableCollection();
公共可观测集合系统{get{return_Systems;}}
公共系统列表视图模型()
{
var systems=SystemAPI.Instance.GetSystems();
foreach(系统中的var系统)
{
系统。添加(系统);
}
}
xaml应该是:

<UserControl x:Name="SystemListScreen">
        <ScrollViewer Grid.Row="1">
                <ItemsControl x:Name="SystemList" ItemsSource="{Binding Path=Systems}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <UniformGrid Columns="4"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Widgets:MultiLineButton 
                                partID="{Binding ID}"
                                company="{Binding Company}"
                                typeSorter="{Binding Name}"
                                typeLocation="{Binding Location}"
                                buttonCommand="{Binding DataContext.navigateInspectList, 
                                    ElementName=SystemListScreen}"
                                buttonCommandParameter="{Binding ItemsSource.ID, 
                                    ElementName=SystemList}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
        </ScrollViewer>
</UserControl>

正如王超所说:

删除
DataType=“SystemModel”
,因为如果只使用一种类型的数据类型作为数据模板,则不需要这样做。正确的语法是
DataType=“vm:SystemModel”
,其中vm在父标记中定义,如:
xmlns:vm=“clr namespace:MySolution.MyVmProject.MyFolder”

此外,请检查以下各项:

DataTemplate
内的绑定中删除
ItemsSource.
,因为它是错误的

仔细检查绑定中的所有名称,因为如果它们是错误的,则在运行时会考虑空值,并且您永远不会知道

检查dataContext,确保
UserControl
将其dataContext连接到包含系统的类型依赖项对象的正确实例。并确保它保持这种状态

  • 不确定,也许您应该从DateTemplate中删除
    DataType=“SystemModel”
  • 或者尝试使用简单的文本框(绑定id)作为数据模板,看看是否还有空的
  • 如果上面没有得到任何帮助,请尝试Snoop(snoopwpf.codeplex.com)看看发生了什么

  • 从绑定中删除“ItemsSource.”。dataContext已设置为ItemsSource.NotifyChanged(“系统”);区分大小写的NotifyChanged(“系统”)感谢Bizz和Eran,但两者都不能解决问题。1。试过了,但不起作用。这是可行的,问题是usercontrol没有绑定到list Item尝试定义ID、Company、Location和Name的dependency属性。定义dependency属性的确切含义是什么?partID等都是依赖属性。我以为你在使用INotifyPropertyChanged和普通对象。在我看来,你遗漏了很多东西,这是因为它变得复杂了。谢谢你的回答,但它仍然不起作用。我得到的错误是:“BindingExpression路径错误:'在'object''MultiLineButton'(Name='')上找不到Company'属性。BindingExpression:path=Company;DataItem='MultiLineButton'(Name='');目标元素是'MultiLineButton'(Name='');目标属性是'Company'(type'String')”将其添加到第一篇文章中。
            public static readonly DependencyProperty buttonCommandProperty = DependencyProperty.Register("buttonCommand", typeof(ICommand), typeof(MultiLineButton));
        public static readonly DependencyProperty buttonCommandParameterProperty = DependencyProperty.Register("buttonCommandParameter", typeof(Object), typeof(MultiLineButton));
        public static readonly DependencyProperty partIDProperty = DependencyProperty.Register("partID", typeof(String), typeof(MultiLineButton));
        public static readonly DependencyProperty companyProperty = DependencyProperty.Register("company", typeof(String), typeof(MultiLineButton));
        public static readonly DependencyProperty typeSorterProperty = DependencyProperty.Register("typeSorter", typeof(String), typeof(MultiLineButton));
        public static readonly DependencyProperty typeLocationProperty = DependencyProperty.Register("typeLocation", typeof(String), typeof(MultiLineButton));
    
        public MultiLineButton()
        { 
            this.DataContext = this;
    
            InitializeComponent();
        }
    
        public String partID
        {
            get { return (String)GetValue(partIDProperty); }
            set { SetValue(partIDProperty, value); }
        }
    
        public String company
        {
            get { return (String)GetValue(companyProperty); }
            set { SetValue(companyProperty, value); }
        }
    
        public String typeSorter
        {
            get { return (String)GetValue(typeSorterProperty); }
            set { SetValue(typeSorterProperty, value); }
        }
    
        public String typeLocation
        {
            get { return (String)GetValue(typeLocationProperty); }
            set { SetValue(typeLocationProperty, value); }
        }
    
        public ICommand buttonCommand
        {
            get { return (ICommand)GetValue(buttonCommandProperty); }
            set { SetValue(buttonCommandProperty, value); }
        }
        public Object buttonCommandParameter
        {
            get { return (Object)GetValue(buttonCommandParameterProperty); }
            set { SetValue(buttonCommandParameterProperty, value); }
        }
    
    private ObservableCollection<SystemModel> _systems = new ObservableCollection<SystemModel>();
    public ObservableCollection<SystemModel> Systems { get { return _systems; } }
    
    public SystemListViewModel()
    {
        var systems = SystemAPI.Instance.GetSystems();
        foreach (var system in systems)
        {
            Systems.Add(system);
        }
    }
    
    <UserControl x:Name="SystemListScreen">
            <ScrollViewer Grid.Row="1">
                    <ItemsControl x:Name="SystemList" ItemsSource="{Binding Path=Systems}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <UniformGrid Columns="4"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Widgets:MultiLineButton 
                                    partID="{Binding ID}"
                                    company="{Binding Company}"
                                    typeSorter="{Binding Name}"
                                    typeLocation="{Binding Location}"
                                    buttonCommand="{Binding DataContext.navigateInspectList, 
                                        ElementName=SystemListScreen}"
                                    buttonCommandParameter="{Binding ItemsSource.ID, 
                                        ElementName=SystemList}"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
            </ScrollViewer>
    </UserControl>