2013-06-11 4 views
2

여기 누군가 나를 기꺼이 돕기를 바랍니다. 나는 꽤 MVVM을 처음 접했고 manny 게시물을 읽고 난 후에도 이것을 이해할 수 없다.MVVM Treeview 선택한 항목

나는 각 항목에 속하는 항목과 계산으로 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> 

얼마나

은 이것이다 : 어떻게 결합 할 본문 내 레이블의 현재 선택한 트리보기 항목에?

<TreeView Name="myTreeview"/> 
<TextBlock Text="{Binding SelectedItem, ElementName=myTreeview, Mode=OneWay}"/> 

그러나이 실제로 당신이 원하는 표시되지 않습니다 : 사전에

덕분에

답변

1

Label (또는 TextBlock)에 TreeViewSelectedItem 바인딩

애디는 매우 간단하다 의 TreeView은 일반적으로 Object이며에 표시하려면 string이 필요합니다..

이 문제를 처리하는 한 가지 방법은 바인딩에 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(); 
     } 
    } 

그런 다음 당신의 TextBlock에 결합은 다음과 같이 표시됩니다

<TextBlock Text="{Binding SelectedItem, Converter={StaticResource GetTextFromItemConverter}, ElementName=myTreeview, Mode=OneWay}"/> 
관련 문제