如何显示ObservableCollection 属性

问题描述 投票:0回答:2

我的代码有一个RightListBox和一个CheckBox(在另一个ListBox下),如下所示:

enter image description here

我想在CheckBox中显示RightListBox项(即T1,T2,T3),但实际上显示的是ViewModel名称。我尝试了多种方式(请参阅我的XAML),但没有一个显示RightListBox项。

这里是代码:

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);
    }
}

这看起来很简单,但我仍然找不到解决方案。所以,请帮帮我。谢谢。

c# wpf checkbox data-binding observablecollection
2个回答
1
投票

您可以使用ListBox.ItemContainerStyle代替ItemTemplate。您应该对Text中的Content属性(而不是GraphViewModel)使用绑定。像这样的东西

<ListBox.ItemTemplate>
    <DataTemplate>                        
        <CheckBox Content="{Binding Text}" x:Name="checkBox"/>                                
    </DataTemplate>
</ListBox.ItemTemplate>

另一个选择是将其与您的其他列表框一起使用<ListBox ItemsSource="{Binding SelectedItems, ElementName=RightListBox}" DisplayMemberPath="Text"/>您还应该将复选框的IsChecked属性绑定到ViewModel属性,目前没有此类属性


0
投票

对于内容,您需要指定要为复选框显示的属性。

<CheckBox Content="{Binding Path=Content.Text,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}" x:Name="checkBox"/>
© www.soinside.com 2019 - 2024. All rights reserved.