0

Depend on my value I need to get oldTemplate or newTemplate for selected item on listview. Code doesn't work, I know that, just want to show what I'd like to achieve

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

Is there any way to get value from my class and bind template?

Thanks

@edit

I've tried like this: create class

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

Now in xaml declare DataContext for class and change Resources as:

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

Gives me error:

Message: Value „Property” must be non-zero.

InerException:

Source: 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()

user13657
  • 745
  • 3
  • 17
  • 36
  • Try to set "ContentTemplate" instead of "Template" – rPulvi May 27 '15 at 09:07
  • But that binging is wrong. Template works without Condition Binding(...) - I dont really know how to do it – user13657 May 27 '15 at 09:13
  • maybe this link can help you http://stackoverflow.com/questions/146269/change-wpf-datatemplate-for-listbox-item-if-selected – rPulvi May 27 '15 at 09:15
  • Ok, just to be sure - I know how to change template for selected item. What I need is choose between two templates depending on additional condition. So if I remove second multitrigger and line with Condigion Binding (...) everything works fine. Problem is that I need to check somehow OSVersion (or get bool value from class) and depend on what I get -> take right template. – user13657 May 27 '15 at 09:24
  • Hmm...I suppose you have to use a custom converter. Your converter has to take the version number as an Input and returns the desired Template – rPulvi May 27 '15 at 09:41

1 Answers1

1

You can achieve this result using a Converter.

Here is an example. I used a window-level property called OSValue; you can use whatever you want:

XAML Style

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

Converter Class

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

Of course this is an example that needs improvements in the way the DataTemplate is retrieved

rPulvi
  • 946
  • 8
  • 33