Я надеюсь, что кто-то хочет помочь мне здесь. Я очень новичок в MVVM, после прочтения manny post и примеров, которые я все еще не могу понять.MVVM Treeview selected item
У меня есть база данных EF, заполненная элементами и вычислениями, относящимися к каждому элементу. i'm показывает элементы и вычисления, используя 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>
Так короче: Как я связать текст моей метки к текущему выбранному элементу дерева?
заранее спасибо
Addy