我有WPF列表框:
<ListBox Name="FileDownloads" SelectionMode="Extended">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Name="Url" Text="{Binding Url}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我喜欢使用以下代码从名称绑定ListBox的能力:
this.OneWayBind(ViewModel, vm => vm.DownloadManager.FileDownloads, v => v.FileDownloads.ItemsSource);
在代码隐藏中绑定有助于重构。
有没有办法使用代码隐藏绑定列表框中的Url texbox?
答案 0 :(得分:4)
有没有办法使用代码隐藏绑定列表框中的Url文本框?
目前不是。您可以像现在一样使用XAML绑定,也可以将数据模板放在UserControls中。
对这种稍微麻烦的方法的一种安慰是,如果您注册数据模板UserControls并在其上实现IViewFor<TViewModel>
:
Splat.Locator.CurrentMutable.Register(typeof(MyView), typeof(IViewFor<MyViewModel>));
然后,您可以将ListBox简单地写为:
<ListBox Name="FileDownloads" SelectionMode="Extended" />
此行将自动为您连接DataTemplate:
this.OneWayBind(ViewModel, vm => vm.DownloadManager.FileDownloads, v => v.FileDownloads.ItemsSource);