简体   繁体   中英

WPF ListView Object DataTrigger

I'm trying to style a rectangle inside of a listview item, based on a data-field of the listview item object.

To return a boolean I'm converting the integer from daydata.workload to a boolean uasing a IValueConverter.

I'm getting no exception, the rectangle is just not affected by the DataTrigger. The other style rules are working fine.

<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; }
}

The problem here is that values entered for the converter parameter and the data trigger value are treated as string. You need to specify the type for each one of these values like shown below:

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

    <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>

Then you can cast converter parameter to an int to make the comparison.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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