WPF 控件样式属性绑定

Xaml页面创建模板控件,绑定颜色关键字 

<Window.Resources>
        <!--创建颜色关键字-->
        <local:DataColorConverter x:Key="DataColorConverterStyle"/>
        <!--创建控件模板-->
        <DataTemplate x:Key="ColorBall">
            <Button Width="40" Height="40" IsEnabled="False" Style="{DynamicResource MetroCircleButtonStyle}" Name="BT_ColorBall" Background="{Binding Path=ColorValue, Converter={StaticResource DataColorConverterStyle}}">
                <Rectangle Width="15" Height="15" Fill="{Binding Path=Background, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}">
                    <Rectangle.OpacityMask>
                        <VisualBrush Stretch="Fill" Visual="{DynamicResource appbar_city}" />
                    </Rectangle.OpacityMask>
                </Rectangle>
            </Button>
        </DataTemplate>
    </Window.Resources>

调用模板控件 

<ListView x:Name="ServiceList">
                    <ListView.View>
                        <GridView>
                             <!--绑定模板控件-->
                            <GridViewColumn Width="50" CellTemplate="{StaticResource ColorBall}"></GridViewColumn>
                            <GridViewColumn Header="Name" Width="200" DisplayMemberBinding="{Binding Name}"></GridViewColumn>
                            <GridViewColumn Header="Status"  Width="80" DisplayMemberBinding="{Binding Status}"></GridViewColumn>
                            <GridViewColumn Header="Log On As" Width="120" DisplayMemberBinding="{Binding LogOnAs}"></GridViewColumn>
                            <GridViewColumn Header="Depends On" Width="120" DisplayMemberBinding="{Binding DependingOn}"></GridViewColumn>
                            <GridViewColumn Header="Description" Width="500" DisplayMemberBinding="{Binding Description}"></GridViewColumn>
                        </GridView>
                    </ListView.View>
                </ListView>

C# 定义转换模式,当listview中当colorvalue值被 “Background="{Binding Path=ColorValue,” 绑定,作为Convert方法第一个参数Value的值,需要强制转换类型后做判断。

 ///// 定义转换器       
    [ValueConversion(typeof(string), typeof(SolidColorBrush))]
    public class DataColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            if ((string)value == "0")
            {
                return new SolidColorBrush(Colors.Red);
            }
            if ((string)value == "1")
            {
                return new SolidColorBrush(Colors.Yellow);
            }
            if((string)value == "2")
            {
                return new SolidColorBrush(Colors.Green);
            }
            return new SolidColorBrush(Colors.Red);
        }

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

 

发布了21 篇原创文章 · 获赞 2 · 访问量 3987
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览