简体   繁体   English

silverlight列表框stackpanel项目通过模板化的项目值定义背景颜色

[英]silverlight listbox stackpanel items define background color by templated item value

I want to chang background color the border or stackpanel by binded item's value 我想通过绑定项目的值更改边框或堆栈面板的背景颜色

 <controls:ChildWindow.Resources>
        <l:StringToColorConverter x:Key="converter"/>
    </controls:ChildWindow.Resources>
...
                <DataTemplate>
                    <StackPanel Orientation="Vertical">

                        <Border BorderBrush="Black" Background="{Binding Cím, Converter={StaticResource converter}}"  x:Name="hatter" BorderThickness="4" CornerRadius="20">
                            <StackPanel Style="{StaticResource dolgozostack}"
                                         Margin="2,2,2,2">
                                <TextBlock Text="{Binding Cím}" TextWrapping="Wrap" Foreground="White" FontSize="13" TextAlignment="Center" />
                            </StackPanel>
                        </Border>
                    </StackPanel>
                </DataTemplate>

And here is my converter: 这是我的转换器:

    public class StringToColorConverter:IValueConverter
        {
            public object Convert(object value, Type targetType,
           object parameter, CultureInfo culture)
            {
                if (value.ToString().ToLower() == "Megvalósítás elindítva".ToLower())
                    return Colors.Green;
                else{
                     SolidColorBrush mybrush=new SolidColorBrush(Colors.Red);
                return mybrush;
}

            }
    }

Find its working with SolidColorBrush 找到它与SolidColorBrush一起使用

You can do that by binding Background property that you want to change to item's value, and use Converter that will return specific color brush given specific value. 您可以通过将要更改为项目值的Background属性绑定在一起,并使用Converter来返回给定特定值的特定颜色笔刷来实现此目的。

<TextBlock Text="{Binding Full_Name}" FontSize="16" TextAlignment="Center" TextWrapping="Wrap"
        Foreground="{Binding Full_Name, Converter={StaticResource ColorConverter}" />

For Reference : 以供参考 :

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM