Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF
First I add a border and tried to change the border color according a value.
 
<Border Height="49" Margin="11,0,0,48" Name="border1" VerticalAlignment="Bottom" Style="{StaticResource changeBorder}" HorizontalAlignment="Left" Width="121" />
 
<Style TargetType="Border" x:Key="changeBorder">
        <Setter Property="BorderBrush" Value="Green" />
        <Setter Property="BorderThickness" Value="4" />
        <Style.Triggers >
            <DataTrigger Binding="{Binding Path=DisplayText}" Value="True">
                <Setter Property="BorderBrush" Value="Black" />
            </DataTrigger>
 
        </Style.Triggers>
    </Style>
 
It worked fine it change the color according to the value of Displaytext.
 
then I add a listview and add a border to listview items and use the same style to change the border color. But Its not working
 

<ListView Margin="167,56,70,39" ItemsSource="{Binding List}" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Border Style="{StaticResource changeBorder}" >
                            <TextBlock Text="Binding List"/>
                        </Border>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
 

<Style TargetType="Border" x:Key="changeBorder">
        <Setter Property="BorderBrush" Value="Green" />
        <Setter Property="BorderThickness" Value="4" />
        <Style.Triggers >
            <DataTrigger Binding="{Binding Path=DisplayText}" Value="True">
                <Setter Property="BorderBrush" Value="Black" />
            </DataTrigger>
 
        </Style.Triggers>
    </Style>
 

Can someone ple tell we whats wrong with this. I want to change the border color of items in the listview at runtime.
 
Thanks
Posted 30-Apr-13 4:05am
Comments
stibee at 30-Apr-13 15:16pm
   
?
Please can you show your "List" class? Does the list element has a "DisplayText" property?
Do you have any binding errors in output window?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Probably need to do a TemplateBinding in the following:
 
<Style TargetType="Border" x:Key="changeBorder">
        <Setter Property="BorderBrush" Value="Green" />
        <Setter Property="BorderThickness" Value="4" />
        <Style.Triggers >
            <DataTrigger Binding="{TemplateBinding Path=DisplayText}" Value="True">
                <Setter Property="BorderBrush" Value="Black" />
            </DataTrigger>
 
        </Style.Triggers>
    </Style>
 
However, I am not sure. Your code seems a bit strange.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

It seems like in the first case, the DataContext of your Border is the class that contains the DisplayText property. But, in the second case the DataContext of your Border is the list's item (and it doesn't contain the DisplayText property).

Try to bind your DataTrigger to the DisplayText property of the DataContext of the ListView ancestor. Something like:

<Style TargetType="Border" x:Key="changeBorder">
        <Setter Property="BorderBrush" Value="Green" />
        <Setter Property="BorderThickness" Value="4" />
        <Style.Triggers >
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}, Path=DataContext.DisplayText}"
                         Value="True">
                <Setter Property="BorderBrush" Value="Black" />
            </DataTrigger>
 
        </Style.Triggers>
</Style>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Afzaal Ahmad Zeeshan 256
1 OriginalGriff 251
2 BillWoodruff 240
3 Maciej Los 230
4 Sergey Alexandrovich Kryukov 195
0 OriginalGriff 6,419
1 Sergey Alexandrovich Kryukov 6,008
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,070


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 2 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100