シンプルな XAML ユーザー コントロールを使用して、DataContext をコード ビハインド (xaml.cs) ファイルに設定したいと思います。
XAML で DataContext と Itemssource を設定したいので、コンボボックスに ListOfCars プロパティを設定できます
XAML
<UserControl x:Class="Sample.Controls.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="85" d:DesignWidth="200">
<Grid Height="85" Width="200" Background="{StaticResource MainContentBackgroundBrush}">
<StackPanel Orientation="Vertical">
<ComboBox Height="23.338" x:Name="CarList" />
</StackPanel>
</Grid>
</UserControl>
コードビハインド
public List<Cars> ListOfCars
{
get { return _store.ListCars(); }
}
つまり、コード ビハインドでこれを行う代わりに、XAML でバインドを設定するにはどうすればよいですか?
public MyControl()
{
InitializeComponent();
_store = new Store();
CarList.ItemsSource = _store.ListCars();
CarList.DisplayMemberPath = "Name";
}