gpt4 book ai didi

c# - ListView 中的 MVVM 分组项目

转载 作者:可可西里 更新时间:2023-11-01 03:08:18 25 4
gpt4 key购买 nike

我不明白我做错了什么。我想在 listView 中对项目进行分组。结果我想看到类似的东西:

enter image description here

它使用 MVVM 模式。这是我的 XAML 代码。

<CollectionViewSource x:Key="EmploeeGroup"                               
Source="{Binding Path=AllEmploees}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="FirstName" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

<ListView AlternationCount="2"
DataContext="{StaticResource EmploeeGroup}"
ItemsSource="{Binding IsAsync=True}" Padding="0,0,0,10">
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0,0,0,5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" BorderBrush="#FFA4B97F"
BorderThickness="0,0,0,1">
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold"
Text="Name: "/>
<TextBlock FontWeight="Bold"
Text="{Binding Path=FirstName}"/>
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
<ListView.View>
<GridView>
<GridViewColumn Width="150"
Header="FirstName"
DisplayMemberBinding="{Binding Path=FirstName}"/>
<GridViewColumn Width="150"
Header="LastName"
DisplayMemberBinding="{Binding Path=LastName}"/>
</GridView>
</ListView.View>
</ListView>

这是我的EmploeeListViewModel.cs

public class EmploeeListViewModel: ViewModelBase
{
readonly EmploeeRepository _emploeeRepository;

private ObservableCollection<EmploeeViewModel> _allmpl;
public ObservableCollection<EmploeeViewModel> AllEmploees
{
get
{
if (_allmpl == null)
{
_allmpl = new ObservableCollection<EmploeeViewModel>();
CreateAllEmploee();
}
return _allmpl;
}
}

public EmploeeListViewModel(EmploeeRepository emploeeRepository)
{
if (emploeeRepository == null)
throw new ArgumentNullException("emploeeRepository");

_emploeeRepository = emploeeRepository;
_emploeeRepository.EmploeeAdded += this.OnEmploeeAddedToRepository;
}

private void CreateAllEmploee()
{
List<EmploeeViewModel> all =
(from emploee in _emploeeRepository.GetEmploees()
select new EmploeeViewModel(emploee)).ToList();
foreach (EmploeeViewModel evm in all)
{
evm.PropertyChanged += this.OnEmploeeViewModelPropertyChanged;
AllEmploees.Add(evm);
}
this.AllEmploees.CollectionChanged += this.OnCollectionChanged;
}

//this.OnCollectionChanged;
//this.OnEmploeeViewModelPropertyChanged;
}

EmploeeViewModel.cs

public class EmploeeViewModel : ViewModelBase
{
#region Fields
Emploee _emploee;
bool _isSelected;
#endregion

#region Constructor
public EmploeeViewModel(Emploee emploee)
{
if (emploee == null)
throw new ArgumentNullException("emploee");
this._emploee = emploee;
}
#endregion

#region Emploee Properties
public bool IsSelected
{
get { return _isSelected; }
set
{
if (value == _isSelected)
return;

_isSelected = value;
base.OnPropertyChanged("IsSelected");
}
}

public string FirstName
{
get { return _emploee.FirstName; }
set
{
if (value == _emploee.FirstName)
return;
_emploee.FirstName = value;
base.OnPropertyChanged("FirstName");
}
}

public string LastName
{
get { return _emploee.LastName; }
set
{
if (value == _emploee.LastName)
return;
_emploee.LastName = value;
base.OnPropertyChanged("LastName");
}
}
#endregion
}
  • 为什么我不能绑定(bind)“FirstName”带有 Expander.Header 的属性文本 block ?
  • 为什么我有反对类型
    MS.Internal.Data.CollectionViewGroupInternal在 Expander.Header 里面(如果我在里面写扩展头Text="{Binding}")>?

我该怎么办 更改我的 XAML 或 .CS 代码以生成 these results

最佳答案

我自己找到了这个问题的答案。

发送到转换器的对象的类型为:MS.Internal.Data.CollectionViewGroupInternal。

主要原因是使用“名称”对组名称进行数据绑定(bind)只是因为 CollectionViewGroupInternal 中的属性包含当前“组集合”所具有的名称(根据您指定的 GroupDescription)。

不重要 PropertyGroupDescription 中的 GropertyName 是什么。您必须始终在 GroupStyle 容器中使用 {Binding Path=Name}。

我只需要更改我的 XAML 中的一个字符串。

来自:

<TextBlock FontWeight="Bold" Text="{Binding Path=FirstName}"/>

收件人:

<TextBlock FontWeight="Bold" Text="{Binding Path=Name}"/>

关于c# - ListView 中的 MVVM 分组项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4237222/

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