You can associate a list with a list of tuples in WPF

If I have List <Tuple <DateTime, DateTime →, can I bind this to a list in wpf? I know, of course, that I can bind to dictionaries or something else, but for this reason I need to send a list that can have the same values, so it makes sense (or seems) to have a list of tuples? Anyone have any thoughts?

+4
source share
2 answers

Of course, you can become attached to it. I shifted the following code:

public partial class MainWindow : Window { private readonly ObservableCollection<Tuple<DateTime, DateTime>> _dates = new ObservableCollection<Tuple<DateTime,DateTime>>(); public ObservableCollection<Tuple<DateTime, DateTime>> Dates { get { return _dates; } } public MainWindow() { DataContext = this; InitializeComponent(); PopulateList(); } private void PopulateList() { _dates.Add(new Tuple<DateTime, DateTime>(DateTime.Now, DateTime.Now)); _dates.Add(new Tuple<DateTime, DateTime>(DateTime.Now, DateTime.Now)); _dates.Add(new Tuple<DateTime, DateTime>(DateTime.Now, DateTime.Now)); } } 

And XAML:

 <Window x:Class="GuiScratch.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <ListBox ItemsSource="{Binding Dates}"/> </Grid> </Window> 

When I run this, I see a list of items with two dates merged into a list of items.

However, whether you want to do this or not, perhaps more depends on the specific context. If the need to have very pluggable types of bindings makes sense (for example, the date time can change to a string or int), this might be a good option. If you do not, I would say that you are better off messing with something more expressive.

+2
source

Yes, you can bind a ListBox to a Tuples collection. However, if there is no reason not to do this, I will have a collection of your own type, since properties set to Tuple are not particularly descriptive.

+5
source

Source: https://habr.com/ru/post/1386382/


All Articles