ListView SelectedItem multitrigger用于条件

时间:2015-05-27 08:24:48

标签: c# wpf listview templates binding

依靠我的价值,我需要为listview上的选定项目获取oldTemplate或newTemplate。代码不起作用,我知道,只是想展示我想要实现的目标

 <Style TargetType="ListViewItem">
                            <Style.Triggers>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="IsSelected" Value="true" />
                                        <Condition Property="Selector.IsSelectionActive" Value="true" />
                                        <Condition Binding="{Binding Source={x:Static s:Environment.OSVersion.Version.Major}}" Value="5"/>
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Template" Value="{StaticResource SelectedTemplateOld}"/>
                                </MultiTrigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="IsSelected" Value="true" />
                                        <Condition Property="Selector.IsSelectionActive" Value="true" />
                                        <Condition Binding="Binding Source={x:Static s:Environment.OSVersion.Version.Major}" Value="6"/>
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Template" Value="{StaticResource SelectedTemplateNew}"/>
                                </MultiTrigger>
                            </Style.Triggers>
                        </Style>

有没有办法从我的类中获取价值并绑定模板?

由于

@edit

我尝试过这样:创建课程

public class BoolTemplate
    {
        public bool BoolTest
        {
            get
            {
                if (Environment.OSVersion.Version.Major < 6)
                    return true;
                else
                    return false;
            }
        }
    }

现在在xaml中为类声明DataContext并将Resources更改为:

 <ListView.DataContext>
                        <additions:BoolTemplate/>
                    </ListView.DataContext>
                    <ListView.Resources>
                        <Style TargetType="ListViewItem">
                            <Style.Triggers>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="IsSelected" Value="true" />
                                        <Condition Property="Selector.IsSelectionActive" Value="true" />
                                        <Condition Binding="{Binding BoolTest}" Value="true"/>
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Template" Value="{StaticResource SelectedTemplateOld}"/>
                                </MultiTrigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="IsSelected" Value="true" />
                                        <Condition Property="Selector.IsSelectionActive" Value="true" />
                                        <Condition Binding="{Binding BoolTest}" Value="true"/>
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Template" Value="{StaticResource SelectedTemplateNew}"/>
                                </MultiTrigger>
                            </Style.Triggers>
                        </Style>
                    </ListView.Resources>

给我错误:

消息:值“属性”必须为非零。

InerException:

来源:PresentationFramework

  

StackTrace:w System.Windows.Condition.Seal(ValueLookupType type)
  w System.Windows.ConditionCollection.Seal(ValueLookupType type)w   System.Windows.MultiTrigger.Seal()w   System.Windows.TriggerCollection.Seal()w   System.Windows.Style.Seal()

1 个答案:

答案 0 :(得分:1)

您可以使用Converter来实现此结果。

这是一个例子。我使用了一个名为OSValue的窗口级属性;你可以随意使用:

XAML风格

<Style TargetType="{x:Type ListViewItem}" x:Key="myStyle">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="ContentTemplate" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=OSValue, Converter={StaticResource tmplConverter}}"/>
        </Trigger>
    </Style.Triggers>
</Style> 

转换器类

public class TemplateConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int osValue = System.Convert.ToInt32(value);

        var myResourceDictionary = new ResourceDictionary();

        myResourceDictionary.Source =
            new Uri("/MyApp;component/Dictionary1.xaml",
                    UriKind.RelativeOrAbsolute);  

        if(osValue < 6)
            return myResourceDictionary["OldTemplate"];
        else
            return myResourceDictionary["NewTemplate"];

    }

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

当然,这是一个需要改进DataTemplate检索方式

的示例