更新数据时触发DataTemplate

时间:2010-08-06 13:00:13

标签: wpf binding

我有一个类似于此页面上的列表框项目的数据模板... link

我想更进一步,做一些事情来突出他们改变时的项目。例如,使用上面链接中的代码,我想在Widget.Quantity更改时触发一些操作。也许制作quiantity项目(没有其他)flash或其他东西。我怎样才能做到这一点?我在下面提供相关代码......

    <Window.Resources>
    <Style x:Key="RoundedItem" TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border CornerRadius="10" BorderBrush="Black" BorderThickness="1" Margin="1">
                        <ContentPresenter />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <DataTemplate DataType="{x:Type local:Widget}">
        <StackPanel Orientation="Horizontal">
            <Label Content="{Binding Name}" />
            <Label Content="{Binding Quantity}" />
        </StackPanel>
    </DataTemplate>
</Window.Resources>
<ListBox ItemsSource="{Binding Widgets}" ItemContainerStyle="{StaticResource RoundedItem}" HorizontalContentAlignment="Stretch" />

3 个答案:

答案 0 :(得分:1)

只需将触发器添加到DataTemplate.Triggers集合即可。

<DataTemplate DataType="{x:Type local:Widget}">
    <StackPanel x:Name="panel" Orientation="Horizontal">
        <Label Content="{Binding Name}" />
        <Label Content="{Binding Quantity}" />
    </StackPanel>

    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding SomeProperty}" Value="True">
            <Setter Property="Background" Value="Yellow" TargetName="panel" />
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

答案 1 :(得分:0)

您可能希望在Widget类中添加一个属性来执行此操作。或者如果Widget是一个模型,您可能希望将其包装在WidgetViewModel类中,并在其上添加“IsFlashing”属性。然后,只要此“IsFlashing”属性为True,就将触发器设置为触发。

答案 2 :(得分:0)

我设法让它与EventTriggers合作。完整的代码如下。总之,事件触发器检测绑定值何时更改,然后短暂地将文本变为橙色。

    <UserControl.Resources>
    <!-- instantiate an instance of the TimeSettingsCollection class -->
    <c:TimeSettingsCollection x:Key="TimeSettingsCollection"/>

    <Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border Name="ItemBorder" BorderBrush="Gray" BorderThickness="1" Margin="3" Padding="7" Background="Transparent">
                        <ContentPresenter />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="ItemBorder" Property="Background" Value="LightBlue" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <DataTemplate DataType="{x:Type c:TimeSettingsItem}">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="80"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <TextBlock Grid.Row="0" Grid.Column="0" Margin="0,0,8,0" 
                       Style="{StaticResource smallTitleStyle}">Pc Name:</TextBlock>
            <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=PcName}" 
                       Style="{StaticResource textStyleTextBlock}"/>
            <TextBlock Grid.Row="1" Grid.Column="0" Margin="0,0,8,0" 
                       Style="{StaticResource smallTitleStyle}">Time:</TextBlock>
            <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=TimeSettings.DateTime, NotifyOnTargetUpdated=True}" 
                       Style="{StaticResource textStyleTextBlock}" x:Name="timeTextBlock">
                <TextBlock.Background>
                    <SolidColorBrush Color="Transparent"/>
                </TextBlock.Background>
                <TextBlock.Triggers>
                    <EventTrigger RoutedEvent="Binding.TargetUpdated">
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation
                                Storyboard.TargetName="timeTextBlock" 
                                Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                                To="Orange"              
                                Duration="0:0:1"
                                AutoReverse="True"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>                    
                </TextBlock.Triggers>
            </TextBlock>
        </Grid>
    </DataTemplate>
</UserControl.Resources>


<DockPanel>
    <ListBox Name="timeListBox" ItemsSource="{Binding Source={StaticResource TimeSettingsCollection}}" ItemContainerStyle="{StaticResource ListBoxItemStyle}" HorizontalContentAlignment="Stretch" IsSynchronizedWithCurrentItem="True">
    </ListBox>
</DockPanel>