lundi 18 novembre 2019

How to show an ObservableCollection

My code has a RightListBox and a CheckBox (under another ListBox), like this:

enter image description here

I'd like to show the RightListBox items in the CheckBox (i.e., T1, T2, T3), but it actually shows the ViewModel name. I tried in many ways (please see my XAML), but none of them shows the RightListBox items.

Here is the code:

MainWindow.xaml.cs

using ListBoxMoveAll.Model;
using ListBoxMoveAll.ViewModel;
using System.Collections.ObjectModel;
using System.Windows;

namespace ListBoxMoveAll
{
    public partial class MainWindow : Window
    {
        public ObservableCollection<GraphViewModel> RightListBoxItems { get; } 
            = new ObservableCollection<GraphViewModel>();

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;

            RightListBoxItems.Add(new GraphViewModel("T1", new Graph(10)));
            RightListBoxItems.Add(new GraphViewModel("T2", new Graph(20)));
            RightListBoxItems.Add(new GraphViewModel("T3", new Graph(30)));
        }
    }
}

MainWindow.xaml

<Window x:Class="ListBoxMoveAll.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ListBoxMoveAll"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="600">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="80" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="5*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <ListBox x:Name="RightListBox" Grid.Row="0" Grid.RowSpan="3" Grid.Column="2"
    ItemsSource="{Binding RightListBoxItems}" DisplayMemberPath="Text"
    SelectionMode="Extended" Margin="0,10"/>
        <StackPanel Grid.Column="4" Grid.Row="0" Grid.RowSpan="3" Margin="0,10">
            <!--<ListBox ItemsSource="{Binding Items, ElementName=RightListBox}">-->
            <!--<ListBox ItemsSource="{Binding Items, ElementName=RightListBox}" DisplayMemberPath="Text">-->
            <!--<ListBox ItemsSource="{Binding SelectedItems, ElementName=RightListBox}">-->
            <ListBox ItemsSource="{Binding SelectedItems, ElementName=RightListBox}" DisplayMemberPath="Text">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="ListBoxItem">
                                    <CheckBox Content="{TemplateBinding Content}" x:Name="checkBox"/>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ListBox.ItemContainerStyle>
            </ListBox>
        </StackPanel>
    </Grid>
</Window>

Graph.cs

namespace ListBoxMoveAll.Model
{
    public class Graph
    {
        public Graph(int step) { Step = step; }

        public int Step { get; set; }
    }
}

GraphViewModel.cs

using ListBoxMoveAll.Model;

namespace ListBoxMoveAll.ViewModel
{
    public class GraphViewModel
    {
        public string Text { get; }
        public Graph Graph { get; }

        public GraphViewModel(string text, Graph graph) => (Text, Graph) = (text, graph);
    }
}

This looks so simple, but I still can't find a solution. So, please help me. Thank you.




Aucun commentaire:

Enregistrer un commentaire