C# ContentControl.ContentTemplateSelector动态选择模板
我在窗口右侧设置了一个ContentControl,并设置了内容绑定项(它的类型是ObservableCollection)。现在我想实现它:如果没有项目,ContentControl选择第一个DataTemplate,并将项目添加到项目中,ContentControl将选择第二个DataTemplate来显示一些信息 像这样: 问题是,当我将一个项目添加到项目中时,ContentControl没有更新和更改DataTemplate,我尝试设置模式、UpdateSourceTrigger等,但失败了。在ViewModel中,删除一个项目后,我使用以下语句,它将正常工作:C# ContentControl.ContentTemplateSelector动态选择模板,c#,wpf,mvvm,contenttemplateselector,C#,Wpf,Mvvm,Contenttemplateselector,我在窗口右侧设置了一个ContentControl,并设置了内容绑定项(它的类型是ObservableCollection)。现在我想实现它:如果没有项目,ContentControl选择第一个DataTemplate,并将项目添加到项目中,ContentControl将选择第二个DataTemplate来显示一些信息 像这样: 问题是,当我将一个项目添加到项目中时,ContentControl没有更新和更改DataTemplate,我尝试设置模式、UpdateSourceTrigger等,但
private void ExecuteDeleteClientCommand()
{
...
if (DeleteClient(item))
{
ObservableCollection<MyViewModel> tmp = TabItems;
TabItems = null;
TabItems = tmp;
}
}
但我想知道为什么它与配合得很好
Edited2
这是delcaration:
public const string ItemsPropertyName = "Items";
private ObservableCollection<MyViewModel> items = new ObservableCollection<MyViewModel>();
public ObservableCollection<SingleClientDetailViewModel> TabItems
{
get { return items; }
set
{
if (items == value) { return;}
RaisePropertyChanging(ItemsPropertyName);
items = value;
RaisePropertyChanged(ItemsPropertyName);
}
}
public const string ItemsPropertyName=“Items”;
私有ObservableCollection项=新ObservableCollection();
公共可观测集合选项卡项
{
获取{返回项;}
设置
{
如果(items==value){return;}
RaisePropertyChange(ItemsPropertyName);
项目=价值;
RaisePropertyChanged(ItemsPropertyName);
}
}
ContentControl
将仅侦听属性更改的事件,而不侦听集合更改的事件。对于此行为,您需要使用ItemsControl
或其任何其他版本,如ListView
作为一种解决方法,您可以为ContentControl
创建样式
,而不是TemplateSelector
,在项上定义DataTrigger
。计数
并相应地设置ContentTemplate
。
大概
<ContentControl Content="{Binding Items}">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate" Value="{StaticResource DefaultDataTemplate}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Items}" Value="{x:Null}">
<Setter Property="ContentTemplate" Value="{StaticResource NullItemDataTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=Items.Count}" Value="0">
<Setter Property="ContentTemplate" Value="{StaticResource NullItemDataTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
您是否在视图模型和项
属性中实现了“INotifyPropertyChanged”接口?我使用mvvmlight。MyViewModel类继承自ViewModelBase。ViewModelBase:ObserveObject:INotifyPropertyChanged。谢谢,这种方法很有效。但是我仍然不知道当我手动调用RaisePropertyChanged(ItemsPropertyName)
时它为什么不起作用。在这种情况下:现在Items.Count是1,我删除了唯一的一个项并调用RaisePropertyChanged(ItemsPropertyName)。你确定ItemsPropertyName
的值是字符串值项且不为空吗。您没有为该属性提供属性声明,因此无法对此进行进一步评论。@SubmarineX检查第一个字母的大小写。
RaisePropertyChanging(ItemsPropertyName);
RaisePropertyChanged(ItemsPropertyName);
public const string ItemsPropertyName = "Items";
private ObservableCollection<MyViewModel> items = new ObservableCollection<MyViewModel>();
public ObservableCollection<SingleClientDetailViewModel> TabItems
{
get { return items; }
set
{
if (items == value) { return;}
RaisePropertyChanging(ItemsPropertyName);
items = value;
RaisePropertyChanged(ItemsPropertyName);
}
}
<ContentControl Content="{Binding Items}">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate" Value="{StaticResource DefaultDataTemplate}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Items}" Value="{x:Null}">
<Setter Property="ContentTemplate" Value="{StaticResource NullItemDataTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=Items.Count}" Value="0">
<Setter Property="ContentTemplate" Value="{StaticResource NullItemDataTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>