Код: Выделить всё
public class FooBar
{
public string Foo { get; set; } = "Default text";
}
< /code>
foobarcontrol.xaml:
< /code>
foobarcontrol.xaml.cs
public partial class FooBarControl : UserControl
{
public static readonly DependencyProperty FooBarProperty = DependencyProperty.Register(
nameof(FooBar),
typeof(FooBar),
typeof(FooBarControl),
new PropertyMetadata(new FooBar())
);
public FooBar FooBar {
get => (FooBar)this.GetValue(FooBarProperty);
set => this.SetValue(FooBarProperty, value);
}
public FooBarControl()
{
InitializeComponent();
}
}
< /code>
mainwindow.xaml
< /code>
mainwindow.xaml.cs
public partial class MainWindow : Window
{
private ObservableCollection FooBars { get; set; } = new ObservableCollection();
public MainWindow()
{
try
{
InitializeComponent();
FooBars.Add(new FooBar() { Foo = "Hello" });
FooBars.Add(new FooBar() { Foo = "World" });
FooBarControlList.DataContext = FooBars;
FooBarList.DataContext = FooBars;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... tatemplate