Telerik Tile背景绑定

时间:2013-09-19 04:02:11

标签: wpf data-binding telerik styles itemcontainerstyle

我正在尝试在RadTileList中绑定Tile的背景,Tiles是从RadTileList的Itemsource上的一个集合创建的,到目前为止我已尝试更改背景Datatemplate中的边框容器,但看起来Tile背景属性正在胜过它。

在上面的代码中,我尝试设置ItemContainerStyle并为背景设置绑定,但没有任何变化,我希望有人可以帮助我。

注意:背景的颜色是字符串var所以我使用转换器,我独立测试

    <telerik:RadTileList ItemsSource="{Binding Modulo.Modulos_Detail}" TileReorderMode="None" 
                             ScrollViewer.HorizontalScrollBarVisibility="Visible">
            <telerik:RadTileList.ItemContainerStyle>
                <Style >
                    <Setter Property="telerik:Tile.TileType" Value="Quadruple" />
                    <Setter Property="telerik:Tile.Background" Value="{Binding .Color, Converter={StaticResource strHexColorConverter}}" />
                </Style>
            </telerik:RadTileList.ItemContainerStyle>
                <telerik:RadTileList.ItemTemplate>
                <DataTemplate>
                    <Border >
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <Image Grid.Row="0"
                                   HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                                   Source="{Binding .Imagenes.Imagen}" Stretch="Uniform"></Image>
                            <TextBlock Grid.Row="1"  Padding="5"
                                   Text="{Binding Descripcion}" FontSize="20" TextWrapping="Wrap" 
                                   VerticalAlignment="Bottom"/>
                            <!--<Image Source="{Binding .LockViewImage, Converter={StaticResource imgBitmapImageConverter}}" />-->
                        </Grid>
                    </Border>
                </DataTemplate>
            </telerik:RadTileList.ItemTemplate>

2 个答案:

答案 0 :(得分:0)

使用 Tile 创建一个单独的样式作为目标类型。然后,您只需要设置背景属性。 (可选)您可以为样式指定名称并在RadTileList上显式设置它。

<UserControl.Resources>
    <ResourceDictionary>
    <Style TargetType="telerik:Tile" BasedOn="{StaticResource TileStyle}">
            <Setter Property="Background" Value="{StaticResource OmegaMainColorBrush}" />
        </Style>
    </ResourceDictionary>
</UserControl.Resources>

答案 1 :(得分:0)

在我看来,您可以在telerik:Tile控件样式中使用布尔附加属性。如果该属性为True,则在后面的代码中创建绑定。你应该关心的唯一一件事是,Tile的内容将保存在那里定义.Color的对象。 这是代码。

样式(将其放入资源部分)

    <Style TargetType="telerik:Tile" BasedOn="{StaticResource {x:Type telerik:Tile}}">
                    <Setter Property="flowConfiguration:TileAttachedProperties.IsTyleTypeBound" Value="True"/>
                    </Setter>

附加的属性代码(带转换器)

public class TileAttachedProperties
{
    public static readonly DependencyProperty IsTyleTypeBoundProperty = DependencyProperty.RegisterAttached(
        "IsTyleTypeBound",
        typeof (bool),
        typeof (TileAttachedProperties),
        new PropertyMetadata(default(bool), IsTyleBoundPropertyChangedCallback));

    private static void IsTyleBoundPropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs args)
    {
        var tile = sender as Tile;
        var isBound = (bool) args.NewValue;
        if(tile == null || isBound == false) return;
        tile.Loaded += TileOnLoaded;
    }

    private static void TileOnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        var tile = sender as Tile;
        if (tile == null) return;
        tile.Loaded -= TileOnLoaded;

        var tileContent = tile.Content;
        if (tileContent == null || tileContent is ItemTypeWrapper == false) return;

        //here we create binding to define if the type of the Tile(single or double)
        var tileTypeBinding = new Binding("IsDouble");
        tileTypeBinding.Source = tileContent;
        tileTypeBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        tileTypeBinding.Converter = new Bool2TileTypeConverter();
        tile.SetBinding(Tile.TileTypeProperty, tileTypeBinding);

        //here we create binding to define the background of the tile
        var tileBackgroundBinding = new Binding("IsDouble");
        tileBackgroundBinding.Source = tileContent;
        tileBackgroundBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        tileBackgroundBinding.Converter = new Bool2BackgroundConverter();
        tile.SetBinding(Tile.BackgroundProperty, tileBackgroundBinding);

    }

    public static void SetIsTyleTypeBound(DependencyObject element, bool value)
    {
        element.SetValue(IsTyleTypeBoundProperty, value);
    }

    public static bool GetIsTyleTypeBound(DependencyObject element)
    {
        return (bool) element.GetValue(IsTyleTypeBoundProperty);
    } 
}

internal class Bool2BackgroundConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var isDouble = (bool)value;
        return isDouble ? new SolidColorBrush(Color.FromArgb(255, 255, 0, 255)) : new SolidColorBrush(Color.FromArgb(255,0, 255, 255));
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

internal class Bool2TileTypeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var isDouble = (bool) value;
        return isDouble ? TileType.Double : TileType.Single;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

看起来如何: here

此致