gpt4 book ai didi

MVVM 树状 View 选定项

转载 作者:行者123 更新时间:2023-12-03 10:17:57 26 4
gpt4 key购买 nike

我希望有人愿意在这里帮助我。
我对 MVVM 很陌生,在阅读了许多帖子和示例之后,我仍然无法弄清楚这一点。

我有 EF 数据库填充属于每个项目的项目和计算。
我正在使用 TreeView 和 HierarchicalDataTemplate 显示项目和计算。
当我单击 TreeView 项目时,我想绑定(bind)要设置的标签文本

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>

简而言之:如何将我的标签文本绑定(bind)到当前选定的 TreeView 项?

提前致谢

艾迪

最佳答案

绑定(bind)SelectedItemTreeViewLabel (或 TextBlock )相当简单:

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

但是,这实际上不会显示您想要的,因为 SelectedItemTreeView通常是 Object你需要一个 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();
}
}

然后绑定(bind)你的 TextBlock看起来像这样:
<TextBlock Text="{Binding SelectedItem, Converter={StaticResource GetTextFromItemConverter}, ElementName=myTreeview, Mode=OneWay}"/>

关于MVVM 树状 View 选定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17046221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com