好吧,我可能會問一個古老的問題,但我沒有得到我的場景中描述的任何一個。WPF中的DataBind Listbox SubSelection
我有一個可以包含多個子對象的Oject。例如。一個項目obejct可以有幾個資源對象。我有一個具有超級子對象的ObservaleCollection(在我的例子中是Resource對象)。我也有Project對象中的另一個ObservableCollection包含現有的孩子。
在WPF窗口應用程序中向用戶展示這一點的最佳方式是什麼?我還需要爲他們提供一種方法來繪製映射。
我最初的想法是使用經典雙列表方法,有兩個列表框,但我不確定單獨操作視圖層是多麼容易。
[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會給我更多可管理的代碼。
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>'
我正在做一個樣本。我會很快得到它。 – Xcalibur37 2012-03-24 01:11:27
謝謝,期待它.. – Adarsha 2012-03-24 01:14:09