MVVM Treeview选中的项目

时间:2013-06-11 14:08:50

标签: mvvm binding treeview selecteditem

我希望有人愿意帮助我。 我对MVVM很陌生,这是一篇经过阅读的manny帖子和例子后我仍然无法解决这个问题。

我的EF数据库中填充了属于每个项目的项目和计算。 我使用treeview和HierarchicalDataTemplate显示项目和计算。 当我点击树视图项目时,我想绑定要设置的标签文本

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>

简而言之:如何将我的标签文本绑定到当前选定的树视图项?

提前致谢

阿迪

1 个答案:

答案 0 :(得分:1)

SelectedItem的{​​{1}}绑定到TreeView(或Label)非常简单:

TextBlock

然而,这实际上并不会显示您想要的内容,因为<TreeView Name="myTreeview"/> <TextBlock Text="{Binding SelectedItem, ElementName=myTreeview, Mode=OneWay}"/> 的{​​{1}}通常是SelectedItem,您需要在TreeView中显示Object {1}}。

处理此问题的一种方法是在绑定上使用string。您可以实现TextBlock并让它从Converter返回所需的字符串。

IValueConverter

然后SelectedItem上的绑定将如下所示:

   class GetTextFromItemConverter : IValueConverter
   {
      object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
         TreeViewItem itm = (TreeViewItem)value;
         string myString = null;
         //Retrieve whatever portion of the TreeViewItem you want to put in myString.
         return myString;
      }

      object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
         throw new NotImplementedException();
      }
   }