1

我有一个看起来像这样的 ListView:

在此处输入图像描述

组合框绑定到 TfsTask 类型的对象的 ObservableCollection。当用户更改选择时,我希望同一行上的控件填充组合框中所选项目中包含的数据。例如,假设任务“Tests”具有特定的Duration值和另一个Remaining值。如果用户在组合框中选择此任务,我想用这些值填充行上的控件。

这是一个问题:如何将其他列中的控件绑定到组合框的选定项。直接绑定到控件时,您可以使用它的 ElementName 来执行此操作,但如何在 DataTemplate 中执行此操作?

这是我在 XAML 中定义我的 ListView 的方式。我只以任务组合框和持续时间文本框为例

<ListView MinHeight="100" Name="m_taskList">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="140" Header="Task" >
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Grid HorizontalAlignment="Stretch">
                            <ComboBox Name="m_taskName" DisplayMemberPath="Name" SelectedValue="{Binding Path=TaskId, Mode=TwoWay}" SelectedValuePath="Id" ItemsSource="{Binding ElementName=This, Path=TfsTasks}/>
                        </Grid>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="140" Header="Duration" >
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Grid >
                            <TextBox Text="{Binding ????}" />
                        </Grid>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <!-- .... -->
        </GridView>
    </ListView.View>
</ListView>
4

1 回答 1

1

它正在工作,但您必须在此解决方案中的对象中添加属性。

试试这个:

XAML 文件:

<Window x:Class="ListViewCombo.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>
        <ListView MinHeight="100" Name="m_taskList" ItemsSource="{Binding Path=MyItems}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="140" Header="Task" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Grid HorizontalAlignment="Stretch">
                                    <ComboBox Name="m_taskName" DisplayMemberPath="Name" SelectedItem="{Binding Path=SelectedItem}" SelectedValuePath="ID" ItemsSource="{Binding Path=Items}" />
                                </Grid>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="140" Header="Duration" >
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Grid >
                            <TextBox MinWidth="150" Text="{Binding Path=SelectedItem.Duration}" />
                        </Grid>
                    </DataTemplate>
                 </GridViewColumn.CellTemplate>
             </GridViewColumn>                   
            </GridView>
        </ListView.View>
    </ListView>
</Grid>
</Window>

视图模型文件:

using System.Collections.ObjectModel;
using Microsoft.Practices.Prism.ViewModel;

namespace ListViewCombo
{
    class MainViewModel : NotificationObject
    {
        public MainViewModel()
        {
            for (int i = 0; i < 3; i++)
            {
                ObservableCollection<Task> Source = new ObservableCollection<Task>();
                for (int j = 0; j < 5; j++)
                {
                    Source.Add(new Task { ID = i, Name = "Name_" + i, Duration = (i + 2) * 6 + (3 * j) });
                }
                MyItems.Add(new TfsTask { ID = i, Items = Source });
            }                        
        }

        private ObservableCollection<TfsTask> _myItems = new ObservableCollection<TfsTask>();
        public ObservableCollection<TfsTask> MyItems
        {
            get { return _myItems; }
            set { _myItems = value; RaisePropertyChanged(() => MyItems); }
        }    
    }

    public class Task
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Duration { get; set; }
    }

    public class TfsTask
    {
        public int ID { get; set; }
        public ObservableCollection<Task> Items { get; set; }
        public Task SelectedItem { get; set; }
    }
}
于 2012-07-31T18:12:10.043 回答