我有一个ListBox,里面有一些ListItems。所有的ListItems都继承自我的BaseViewModel (使用MVVM)
我想要做的是,如果ListItem的类型是DogViewModel (继承BaseViewModel),那么ListItem就会淡入。如果LIstItem的类型是CatViewModel,那么它将不会有“特殊”效果。
最初,所有值都是cat类型,但用户将类型更改为dog,此时我希望看到原来的值淡出,新的值淡入。目前,我只关注淡入。
下面的代码无法编译,但我希望它能显示我正在尝试实现的目标(以及我尝试过的目标)。
<ListBox.ItemTemplate>
<DataTemplate>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Group}" Value="{x:Type vm:DogVm}">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" From="0.0" To="1.0" Duration="0:0:0.2" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>上面的问题是它完全删除了我的ListItem (或者让它看不见,不确定是哪一个)
我如何在中指定,仅在XAML中,如果刚刚添加到ObseravableCollection中的项是N,则淡入?
发布于 2015-10-30 17:19:00
我认为DataTrigger比EventTrigger更合适,您需要使用x:Type来指定绑定到项的数据。
编辑:我根据你最初的帖子回答了你的问题。现在,根据您当前的代码,我建议使用转换器。
在这里,我选择了我的ItemTemplate作为一个按钮。
<Window.Resources>
<d:ObjectToTypeConverter x:Key="convertToType" />
</Window.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<Button x:Name="btn" Content="{Binding Name}"></Button>
<DataTemplate.Triggers>
<DataTrigger
Binding="{Binding Converter={StaticResource convertToType}}" Value="{x:Type d:DogVm}">
<Setter TargetName="btn" Property="Background" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding Converter={StaticResource convertToType}}" Value="{x:Type d:CatVm}">
<Setter TargetName="btn" Property="Background" Value="Yellow"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>这是转换器..。
public class ObjectToTypeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value.GetType();
}
.....
}发布于 2015-10-30 18:30:46
您应该使用DataTemplateSelector。下面是一个实现:
<Window.Resources>
<DataTemplate x:Key="FirstTemplate">
<TextBlock Text="{Binding Path=YourProperty}"/>
</DataTemplate>
<DataTemplate x:Key="SecondTemplate">
<TextBlock Text="{Binding Path=YourProperty}" Background="Red"/>
</DataTemplate>
</Window.Resources>和DataTemplateSelector:
public class ViewModelDataTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
FrameworkElement element;
//System.Data.DataRowView viewModelItem;
element = container as FrameworkElement;
// Tests to ensure that both the item and the container are not null, and that the
// item is of the expected data type.
if (element != null && item != null && item is System.Data.DataRowView)
{
// Returns the FirstTemplate in the case of a match
if (item is PersonViewModel)
return element.FindResource("FirstTemplate") as DataTemplate;
else
// Returns the SecondTemplate for other cases
return element.FindResource("SecondTemplate") as DataTemplate;
}
return null;
}
}
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525" >
<Window.Resources>
<my:ViewModelDataTemplateSelector x:Key="myTemplateSelector" />
</Window.Resources>
<!-- The code omitted for brevity -->
</Window>要选择DataTemplateSelector,您应该在ListBox上使用它:
<ListBox ItemTemplateSelector="{StaticResource myTemplateSelector}" />https://stackoverflow.com/questions/33432007
复制相似问题