WPF ListView对象DataTrigger

时间:2017-12-25 14:37:39

标签: c# wpf datatrigger ivalueconverter

我试图根据listview项目对象的数据字段在listview项目中设置矩形样式。

返回一个布尔值我将daydata.workload中的整数转换为一个支持IValueConverter的布尔值。

我没有异常,矩形不受DataTrigger的影响。其他风格规则工作正常。

<Window.Resources>
    <cv:numConverter x:Key="capacityConverter" />
<Window.Resources>

-

<ListView Name="weekView" ItemsSource="{Binding dayList}" ItemTemplate="{StaticResource DefaultTemplate}" >
            <ListView.Resources>
                <Style TargetType="Rectangle" x:Key="capacityBG">

                    <Setter Property="Stroke" Value="#FFE2E2E2" />
                    <Setter Property="Width" Value="180" />
                    <Setter Property="Height" Value="10" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=dayList.workload, Converter={StaticResource capacityConverter}, ConverterParameter=12}">
                            <DataTrigger.Value>true</DataTrigger.Value>
                            <Setter Property="Fill" Value="Red"/>
                        </DataTrigger>

                    </Style.Triggers>
                </Style>
            </ListView.Resources>
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"></StackPanel>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="Background" Value="Transparent" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListViewItem}">
                                <ContentPresenter />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>

-

<Rectangle Style="{DynamicResource capacityBG}" VerticalAlignment="Top" Grid.Row="0" />

-

  public class numConverter : IValueConverter
{

    object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((int)value) > val;
    }

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

    public int val { get; set; }
}

-

public class dayData
{
    public DateTime date { get; set; }
    public int workload { get; set; }

    public List<job> jobs { get; set; }
}

1 个答案:

答案 0 :(得分:1)

这里的问题是为转换器参数输入的值和数据触发值被视为字符串。您需要为每个值指定类型,如下所示:

textBlock1.Text = "You Entered: " + **textBox1.Text**;

xmlns:sys="clr-namespace:System;assembly=mscorlib"

然后你可以将转换器参数转换为int来进行比较。

    <Style.Triggers>
            <DataTrigger>
                <DataTrigger.Binding>
                    <Binding Path="WorkLoad" Converter="{StaticResource capacityConverter}">
                        <Binding.ConverterParameter>
                            <sys:Int32>12</sys:Int32>
                        </Binding.ConverterParameter>
                    </Binding>
                </DataTrigger.Binding>
                <DataTrigger.Value>
                    <sys:Boolean>true</sys:Boolean>
                </DataTrigger.Value>
                <Setter Property="Fill" Value="Red"/>
            </DataTrigger>
    </Style.Triggers>