DataTemplate中的背景绑定

时间:2011-12-07 01:32:30

标签: wpf xaml binding resources converter

我遇到了以下问题。 我有一个ListBox,其中的项目由DataTemplate模板化。此模板有一个网格,我希望该网格的背景更改对应的值。该值由转换器计算,转换器将传递在Grids Resources中创建的CollectionView(它需要在DataTemplate中创建,因为数据取决于ListBox中选择的名称)。

我无法让它工作! 我尝试了很多不同的东西,但我认为问题在于我想在资源不可用的级别上访问Grid的资源(Grid.Background)。另一方面,当我将CollectionView移动到DataTemplate的资源时,我似乎无法访问该资源(转换器不再工作)...我希望我已经让它变得可以理解我的目标为^^。

以下是所有相关代码:

<Window.Resources>
    <local:newConverter x:Key="newConverter" />

    <Style x:Key="personButton" TargetType="{x:Type Button}">
        <Setter Property="Visibility" Value="Hidden" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsMouseOver}" Value="True">
                <Setter Property="Visibility" Value="Visible" />
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsMouseOver}" Value="False">
                <Setter Property="Visibility" Value="Hidden" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

    <DataTemplate x:Key="listBoxTemplate" >
        <Grid Name="personGrid" Margin="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="1*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <Grid.Resources>
                <CollectionViewSource x:Key="items" Source="{Binding XPath=item}">
                    <CollectionViewSource.GroupDescriptions>
                        <PropertyGroupDescription PropertyName="@name"/>
                    </CollectionViewSource.GroupDescriptions>
                </CollectionViewSource>
            </Grid.Resources>

            <WrapPanel Grid.Row="0" Grid.Column="2" HorizontalAlignment="Right">
                <Button Grid.Row="0" Grid.Column="2" Margin="1" Content="&#x2713;" Style="{StaticResource personButton}" VerticalAlignment="Center" Background="Transparent" Click="button_person_balanced"/>
                <Button Grid.Row="0" Grid.Column="2" Margin="1" Content="&#x00D7;" Style="{StaticResource personButton}" VerticalAlignment="Center" Background="Transparent" Click="button_person_delete"/>
            </WrapPanel>
            <Label Grid.Row="0" Grid.Column="1" Content="{Binding Source={StaticResource items}, Converter={StaticResource newConverter}, ConverterParameter=items}" />
            <Label Grid.Row="1" Grid.Column="0" Content="{Binding Source={StaticResource items}, Converter={StaticResource newConverter}, ConverterParameter=date}" />
            <Label Grid.Row="1" Grid.Column="2" Content="{Binding Source={StaticResource items}, Converter={StaticResource newConverter}, ConverterParameter=sum}" HorizontalAlignment="Right" />

            <Grid.Background>
                <Binding Source="{StaticResource items}" Converter="{StaticResource newConverter}" ConverterParameter="background" />
            </Grid.Background>

        </Grid>
    </DataTemplate>
</Window.Resources>

<ListBox Name="PersonListBox" ItemTemplate="{StaticResource listBoxTemplate}" DataContext="{Binding}">
            <ListBox.Resources>
                <CollectionViewSource x:Key="SortedItems" Source="{Binding}">
                    <CollectionViewSource.SortDescriptions>
                        <scm:SortDescription PropertyName="@name"/>
                    </CollectionViewSource.SortDescriptions>
                </CollectionViewSource>
            </ListBox.Resources>

            <ListBox.ItemsSource>
                <Binding Source="{StaticResource SortedItems}"/>
            </ListBox.ItemsSource>
        </ListBox>

这里是转换器的代码(一切正常,背景部分确实返回正确的值,但背景的颜色没有设置(返回一个简单的字符串足够背景)):

class newConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        CollectionView items = value as CollectionView;
        if (items == null)
            return null;
        float sum = 0;
        foreach (XmlNode node in items)
        {
            sum += float.Parse(node.Attributes.GetNamedItem("value").InnerText);
        }

        switch ((string)parameter)
        {
            case "sum":
                return sum.ToString("C");

            case "items":
                return items.Groups.Count + " Items";

            case "date":
                DateTime minDate = DateTime.MinValue;
                foreach (XmlNode node in items)
                {
                    if (DateTime.Parse(node.Attributes.GetNamedItem("date").InnerText) >= minDate)
                        minDate = DateTime.Parse(node.Attributes.GetNamedItem("date").InnerText);
                }
                if (items.Count == 0)
                    return DateTime.Now.ToShortDateString();
                else
                    return "noDateSet";

            case "background":
                if(sum > 0)
                    return "LightGreen";
                if (sum < 0)
                    return "IndianRed";
                else
                    return "SkyBlue";

            default:
                return "missing parameter";
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

我非常感谢任何帮助,我现在正坐在这件事上好几天哦:)

此致

0 个答案:

没有答案