2
我希望有人願意幫助我。 我對MVVM很新穎,閱讀完曼尼貼子後,我仍然無法弄清楚這一點。MVVM Treeview選定項目
我有EF數據庫填寫項目和計算屬於每個項目。 我使用treeview和HierarchicalDataTemplate顯示項目和計算。 當我點擊一個TreeView項目,我想一個標籤的文本綁定在
public string totaalPrijs
進行設置,但我只是無法弄清楚如何做到這一點!
這是我CalculationViewModel外觀
namespace Treeview_test1.ViewModel
{
public class CalculationViewModel : ViewModelBase
{
public CalculationViewModel(TableItemChildren child)
{
this.Child = child;
IsChecked = false;
}
public TableItemChildren Child { get; protected set; }
public string totaalPrijs
{
get { return Child.dbTotaalPrijs; }
set
{
if (Child.dbTotaalPrijs != value)
{
Child.dbTotaalPrijs = value;
RaisePropertyChanged("totaalPrijs");
}
}
}
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set
{
if (_isChecked != value)
{
_isChecked = value;
RaisePropertyChanged("IsChecked");
}
}
}
}
,這裏是我的ItemViewModel
namespace Treeview_test1.ViewModel
{
public class ItemViewModel : ViewModelBase
{
public ItemViewModel()
{
calcVMColl = new ObservableCollection<CalculationViewModel>();
foreach (TableItemChildren calc in Service.getItemCalculations("1"))
{
calcVMColl.Add(new CalculationViewModel(calc));
}
}
// Switch between real and mock data
private IGetCalculations _service;
public IGetCalculations Service
{
get
{
if (_service == null)
{
if (IsInDesignMode)
_service = new MockCalculations();
else
_service = new GetCalculations();
}
return _service;
}
set
{
_service = value;
}
}
private ObservableCollection<CalculationViewModel> _calcVMColl;
public ObservableCollection<CalculationViewModel> calcVMColl
{
get { return _calcVMColl; }
set
{
if (calcVMColl != value)
{
_calcVMColl = value;
RaisePropertyChanged("calcVMColl");
}
}
}
}
和XAML
<Window x:Class="Treeview_test1.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" xmlns:ViewModel="clr-namespace:Treeview_test1.ViewModel">
<Window.DataContext>
<ViewModel:ItemViewModel />
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="204" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TreeView x:Name="tree" Width="195" HorizontalAlignment="Left" ItemsSource="{Binding calcVMColl}" Background="LightGray" Grid.Column="0" RenderTransformOrigin="1.016,0.509">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsSelected" Value="{Binding IsChecked, Mode=TwoWay}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontSize" Value="10" />
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<DataTemplate DataType="{x:Type ViewModel:CalculationViewModel}">
<Label Content="{Binding totaalPrijs}" />
</DataTemplate>
</TreeView.Resources>
</TreeView>
<Label Grid.Column="1" HorizontalAlignment="Left" Margin="39,39,0,0" VerticalAlignment="Top" Content="{Binding....?}" Foreground="Black" FontFamily="segeo ui" FontSize="20" />
</Grid>
因此,在短期:如何綁定文本 我的標籤到當前選定的樹視圖項?
在此先感謝
阿迪