【发布时间】:2012-03-24 00:14:35
【问题描述】:
好的,我可能会问一个古老的问题,但我没有得到其中任何一个描述的场景。
我有一个可以包含多个子对象的对象。例如。一个 Project 对象可以有多个 Resource 对象。我有一个带有超级组子对象的 ObservaleCollection(在我的例子中是 Resource 对象)。我在包含现有子项的 Project 对象中还有另一个 ObservableCollection。
在 WPF windows 应用程序中向用户展示此内容的最佳方式是什么?我还需要为他们提供一种更改映射的方法。
我最初的想法是使用带有两个列表框的经典双列表方法,但我不确定单独操作视图层是否容易。
[Resoure Collection] [Resoure Collection in a Project]
-------------------- ---------------------------------
|Resource 1 | > |Resource 3 |
|Resource 2 | >> |Resource 4 |
|Resource 5 | < | |
|Resource 6 | << | |
|Resource 7 | | |
对于不同对象的 4 个类似映射,我需要类似的 UI。我试图将其移至用户控件,但看起来我不能在 UserControl 中拥有通用集合(私有 ObservableCollection)。
有经验的会员有什么想法吗?
/************ *************** *************** ********/ 编辑:这是我到目前为止所得到的,请注意我将使用 UserControl,因为我需要在多个屏幕中使用相同的 UI,而且我觉得 UserCONtrol 会给我提供更易于管理的代码。
用户控件的 XAML
<UserControl 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"
mc:Ignorable="d"
x:Class="TimeTracker.ItemsSelectionLists"
x:Name="ItemsSelectionControl">
<Grid x:Name="LayoutRoot">
<Grid Background="#FFF9FDFD"
Margin="0,0,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock x:Name="SourceHeading"
Grid.Column="0"
Margin="8,8,0,0"
TextWrapping="Wrap"
Text="Whole Team"
VerticalAlignment="Top" />
<ListBox x:Name="SourceItemsList"
Grid.Column="0"
Margin="8,30,8,8"
MinWidth="150"
SelectionMode="Multiple"
ItemsSource="{Binding Path=Collection1}"/>
<StackPanel Grid.Column="1"
Margin="0"
Orientation="Vertical"
VerticalAlignment="Center">
<Button Content=">"
Height="25"
Width="25" />
<Button Content=">>"
Height="25"
Width="25" />
<Button Content="<"
Height="25"
Width="25" />
<Button Content="<<"
Height="25"
Width="25" />
</StackPanel>
<TextBlock x:Name="TargetHeading"
Grid.Column="2"
Margin="8,8,8,0"
TextWrapping="Wrap"
Text="Current Team"
VerticalAlignment="Top" />
<ListBox x:Name="SelectedItemsList"
Grid.Column="2"
Margin="8,30,8,8"
MinWidth="150"
ItemsSource="{Binding Path=Collection2}"/>
</Grid>
</Grid>
</UserControl>
代码:
/// <summary>
/// Interaction logic for ItemsSelectionLists.xaml
/// </summary>
public partial class ItemsSelectionLists: UserControl
{
[Bindable(true)]
internal ObservableCollection<TrackerItem> SourceList
{
get
{
return _vm.Collection1;
}
set
{
_vm.Collection1 = value;
}
}
private readonly ViewModel _vm;
public ItemsSelectionLists()
{
this.InitializeComponent();
_vm = new ViewModel();
this.DataContext = _vm;
}
}
public class ViewModel : INotifyPropertyChanged
{
#region Properties
private ObservableCollection<TrackerItem> _collection1;
/// <summary>
/// This is the first collection.
/// </summary>
internal ObservableCollection<TrackerItem> Collection1
{
get
{
return _collection1;
}
set
{
if (value != _collection1)
{
_collection1 = value;
NotifyPropertyChanged("Collection1");
}
}
}
private ObservableCollection<TrackerItem> _collection2;
/// <summary>
/// This is the second collection.
/// </summary>
internal ObservableCollection<TrackerItem> Collection2
{
get
{
return _collection2;
}
set
{
if (value != _collection2)
{
_collection2 = value;
NotifyPropertyChanged("Collection2");
}
}
}
#endregion
#region Constructors
/// <summary>
/// Default constructor.
/// </summary>
public ViewModel()
{
// Create initial collections.
// Populate first collection with sample data
_collection1 = new ObservableCollection<TrackerItem>();
// Seconf collection is empty
_collection2 = new ObservableCollection<TrackerItem>();
}
#endregion
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
}
主窗口
<TabItem Header="Resource Allocation">
<local:ItemsSelectionLists x:Name="ResourceSelection"/>
</TabItem>
代码
ResourceSelection.SourceList = MainObject.Resources;
//error CS0029: Cannot implicitly convert type 'System.Collections.ObjectModel.ObservableCollection<TimeTracker.Resource>' to 'System.Collections.ObjectModel.ObservableCollection<TimeTracker.TrackerItem>'
【问题讨论】:
-
我正在制作一个样本。我很快就会得到它。
-
谢谢,期待..
标签: c# wpf user-controls listbox