Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind data inside ListBox using code-behind in ReactiveUI

Tags:

reactiveui

I have WPF listbox:

<ListBox Name="FileDownloads" SelectionMode="Extended">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <TextBlock Name="Url" Text="{Binding Url}" />
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

I like the ability to bind ListBox by name from code behind using: this.OneWayBind(ViewModel, vm => vm.DownloadManager.FileDownloads, v => v.FileDownloads.ItemsSource); Having binding in code-behind helps with refactoring.

Is there any way to bind Url texbox inside the listbox using code-behind?

like image 566
Zelid Avatar asked Aug 23 '14 20:08

Zelid


1 Answers

Is there any way to bind Url textbox inside the listbox using code-behind?

Not at the moment. You can either use XAML bindings like you're doing now, or you can put your data templates inside UserControls.

A consolation to this somewhat more cumbersome approach, is that if you register your data template UserControls and implement IViewFor<TViewModel> on them:

Splat.Locator.CurrentMutable.Register(typeof(MyView), typeof(IViewFor<MyViewModel>));

Then, you can write the ListBox simply as:

<ListBox Name="FileDownloads" SelectionMode="Extended" />

This line will automatically wire up a DataTemplate for you:

this.OneWayBind(ViewModel, vm => vm.DownloadManager.FileDownloads, v => v.FileDownloads.ItemsSource);
like image 88
Ana Betts Avatar answered Oct 17 '22 11:10

Ana Betts