zoukankan      html  css  js  c++  java
  • WPF 根据绑定值调用不同的样式

           有些情况,元素的样式要随着绑定的值不而改变,下面为实现方法:

     
            样式:
            p,s. 最好定义为全局样式,不然之后用到的转换器可能会找不到.
     
            <Style x:Key="border1" TargetType="Border">
                <Setter Property="Background">
                    <Setter.Value>
                        <LinearGradientBrush>
                            <GradientStop Color="#f00" Offset="0.5"/>
                            <GradientStop Color="#fff" Offset="1"/>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style x:Key="border2" TargetType="Border">
                <Setter Property="Background">
                    <Setter.Value>
                        <LinearGradientBrush>
                            <GradientStop Color="#00f" Offset="0.5"/>
                            <GradientStop Color="#fff" Offset="1"/>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
            </Style>
     
            
            转换器:
        
            public class BGConvert : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                int age = (int)value;
                Style style = new Style();
                FrameworkElement fe = new FrameworkElement();
                style = (Style)fe.TryFindResource("border1");
                if (age % 2 == 0)
                {
                    style = (Style)fe.TryFindResource("border1");
                }
                else
                {
                    style = (Style)fe.TryFindResource("border2");
                }
                return style;
            }
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
     
            页面文件:
     
            <Window x:Class="StyleForBG.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:convert="clr-namespace:StyleForBG"
            Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
        <Window.Resources>
            //定义资源
            <convert:BGConvert x:Key="conver"/>        
        </Window.Resources>
        <Grid>
            <ListView x:Name="lvTest" ItemsSource="{Binding}" Width="500" Height="300">
                <ListView.ItemTemplate>
                    <DataTemplate>
                                                                                  //使用转换器
                        <Border BorderBrush="Black" Style="{Binding StuAge, Converter={StaticResource conver}}" BorderThickness="1" Width="120" Height="60">
                            <TextBlock Text="{Binding StuName}" />
                        </Border>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </Window>
  • 相关阅读:
    STL堆算法性能分析与优化方法(GCC4.4.2 stl_heap.h源代码分析与改进方案)
    POJ 1631 Bridging Singnals
    一个用于读unicode文本的迭代器(iterator)
    常用文本压缩算法及实现(To be finshed!)
    volatile语义及线程安全singleton模式探讨
    C++实现的huffman与canonical huffman的压缩解压缩系统,支持基于单词的压缩解压缩
    linux环境下 C++性能测试工具 gprof + kprof + gprof2dot
    多线程统计多个文件的单词数目C++0x多线程使用示例
    python嵌入C++ boost.python如何在C++中调用含有不定长参数tuple变量和关键字参数dict变量的函数
    boost.python入门教程 python 嵌入c++
  • 原文地址:https://www.cnblogs.com/Dincat/p/2509043.html
Copyright © 2011-2022 走看看