Wpf 视图模型、依赖项属性混乱

Wpf 视图模型、依赖项属性混乱,wpf,view,model,dependencies,properties,Wpf,View,Model,Dependencies,Properties,我正在开发一个自定义WPF控件,但不知道如何使用dependency属性。我的视图模型包含两个属性: class Customer { string Name; string ID; } 我的自定义控件负责显示这些字段 问题1:我需要在自定义控件中定义任何依赖项属性(例如“名称”、“ID”)吗 问题2:我正在使用ItemsControl显示客户列表。如何将Customer对象传递到我的自定义控件?是通过DataContext完成的,还是需要在控件和xaml中添加“Customer”依赖

我正在开发一个自定义WPF控件,但不知道如何使用dependency属性。我的视图模型包含两个属性:

class Customer {
  string Name;
  string ID;
}
我的自定义控件负责显示这些字段

问题1:我需要在自定义控件中定义任何依赖项属性(例如“名称”、“ID”)吗

问题2:我正在使用ItemsControl显示客户列表。如何将Customer对象传递到我的自定义控件?是通过DataContext完成的,还是需要在控件和xaml中添加“Customer”依赖属性,将“Customer”绑定到“某物”(那是什么东西)


Q1。为什么有自定义控件?通常,您只需创建一个
UserControl
(WPF中
UserControl
和自定义控件之间有区别)并将
UserControl
中的属性绑定到视图模型的属性。例如(我们称之为
CustomerView
):


ItemsControl
生成的每个项都将相关数据项设置为其
DataContext
。因此,每个
CustomerView
都将有相应的
Customer
作为其
DataContext

通常,您创建自定义控件并不仅仅是为了显示一些数据,而是使用它们。
<ItemPresenter>
   <MyCustomControl  Customer="??what should i put here???"/>
</ItemPresender>
<UserControl ...>
    <StackPanel>
        <TextBlock Text="{Binding ID}"/>
        <TextBox Text="{Binding Name}"/>
    </StackPanel>
</UserControl>
<ItemsControl ItemsSource="{Binding Customers}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <local:CustomerView/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>