ListView 绑定 更改通知

后八十生人 2012-07-10 04:19:27
我为一个ListView定义了如下的CollectionViewSource:

<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="GroupSettingID" Converter="{StaticResource GetGroupSettingByPlayerConverter}"/>
</CollectionViewSource.GroupDescriptions>

其中的
PropertyGroupDescription PropertyName="GroupSettingID" Converter="{StaticResource GetGroupSettingByPlayerConverter}"/>
指向一个自定义的 GroupSettingID , 转换器通过此ID在一个列表中将与ID对应的GroupSetting的Name属性返回。

有没有什么办法,当对应的GroupSetting中的Name属性改变时,PropertyGroupDescription的属性一起改变?
...全文
130 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
后八十生人 2012-07-12
  • 打赏
  • 举报
回复
问题解决了,谢谢各位。
namhyuk 2012-07-12
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
有没有什么办法,当对应的GroupSetting中的Name属性改变时,PropertyGroupDescription的属性一起改变?[/Quote]
一直没弄明白这句话的意思。就比如如下这种情况吧。



三个表,雇员(Employee)、部门(Department)、兴趣(Hobby)。

我可以把雇员表接部门分组显示,可以按兴趣分组显示,也可以按部门、兴趣的组合分组显示。但这种情况下,按楼主的表达,GroupSettingId这个字段又貌似不应该出现在Employee表里。所以我在这里就跟不上楼主思路了。就是说我不知道GroupSettingId是哪儿来的。

namhyuk 2012-07-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
GroupDescriptions必须Add到CVS的GroupDescriptions上。[/Quote]

其实VM向View提供ICollectionView,并且在此ICollectionView上应用GroupDescriptions的思路没问题。

private ICollectionView _employees;
public ICollectionView Employees
{
get
{
if (_employees == null)
this._employees = CreateView(this._Context.Employees);
return _employees;
}
}
public ICollectionView CreateView(IEnumerable source)
{
CollectionViewSource cvs = new CollectionViewSource();
cvs.Source = source;
return cvs.View;
}
//……
//然后在必要的地方
Employees.GroupDescriptions.Clear();
Employees.GroupDescriptions.Add(new PropertyGroupDescription(currentSetting));


这是没问题的。
namhyuk 2012-07-11
  • 打赏
  • 举报
回复
ICollectionView就是在ViewModel里呀。
xhtmldivcss 2012-07-11
  • 打赏
  • 举报
回复
前台也就是个ComboBox和DataGrid.

<StackPanel>
<ComboBox HorizontalAlignment="Left" Name="comboBox1" VerticalAlignment="Top" ItemsSource="{Binding GroupSettings}" SelectedItem="{Binding CurrentSetting, Mode=TwoWay}" />
<sdk:DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left" ItemsSource="{Binding CollectionView}" Name="employeeDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" >
<sdk:DataGrid.Columns>
<sdk:DataGridCheckBoxColumn x:Name="applyNewNumberColumn" Binding="{Binding Path=ApplyNewNumber}" Header="Apply New Number" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="AColumn" Binding="{Binding Path=……}" Header="Apply Number" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="BColumn" Binding="{Binding Path=……}" Header="Current Number" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="CColumn" Binding="{Binding Path=……}" Header="Department Id" Width="SizeToHeader" />
</sdk:DataGrid.Columns>
……
</sdk:DataGrid>
</StackPanel>
xhtmldivcss 2012-07-11
  • 打赏
  • 举报
回复

public class MainPageViewModel : NotificationObject
{
private XXXDomainContext _Context = new XXXDomainContext();
private CollectionViewSource cvs;

IEnumerable<string> groupSettings;

public IEnumerable<string> GroupSettings
{
get
{
if (groupSettings == null)
groupSettings = new List<string> { "分组条件1", "分组条件2" };
return groupSettings;
}
}

private string currentSetting;

public string CurrentSetting
{
get { return currentSetting; }
set
{
if (currentSetting != value)
{
currentSetting = value;
RaisePropertyChanged(() => this.CurrentSetting);
cvs.GroupDescriptions.Clear();
cvs.GroupDescriptions.Add(new PropertyGroupDescription(currentSetting));
}
}
}
public MainPageViewModel()
{
if (!DesignerProperties.IsInDesignTool)
_Context.Load(_Context.GetXXXQuery(), lo => { CurrentSetting = "分组条件2"; }, null);
}
public ICollectionView CollectionView
{
get { return CreateView(_Context.XXX); }
}
public ICollectionView CreateView(IEnumerable source)
{
if (cvs == null)
cvs = new CollectionViewSource();
cvs.Source = source;
return cvs.View;
}


根据ComboBox的选项,更新DataGrid的分组显示。

把XXXContext, GetXXXQuery, _Context.XXX改为你成对应的……
xhtmldivcss 2012-07-11
  • 打赏
  • 举报
回复
中午我大概的试了一下。把CollectionViewSource放在VM里。
然后当Group条件变化时,比如ComboBox(显示Group条件)双向绑定VM里的自定义的某个属性,当这个属性变化时在它的set{}里先把CVS.GroupDescriptions.Clear(),然后再Add新选择的条件。界面会立即反应,无需ICollectionView.Clear().

顺便说一下,我做的过程中有个东西绊了我好长时间。就是CollectionViewSource严格讲并不是ICollectionView的实现(对应的PagedCollectionView,DomainCollectionView应该是吧)。所以GroupDescriptions必须Add到CVS的GroupDescriptions上。而我开始时,是这样做的。


public ICollectionView CollectionView
{
get
{
return CreateView(_Context.MyEntities);
}
}
public ICollectionView CreateView(IEnumerable source)
{
CollectionViewSource cvs = new CollectionViewSource();
cvs.Source = source;
return cvs.View;
}

然后我一直试图通过CollectionView来增加GroupDescriptions,结果失败。
后八十生人 2012-07-10
  • 打赏
  • 举报
回复
如何在NotifyPropertyChanged时ICollectionView.Refresh()?

最好在UI和代码耦合度最低的情况下实现。
namhyuk 2012-07-10
  • 打赏
  • 举报
回复
是不是CollectionViewSource或者ICollectionView.Refresh()一下的问题呀?

8,757

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧