Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
3.29/5 (3 votes)
See more:
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" />


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


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



XML
<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
Comments
stibee 30-Apr-13 15:16pm    
<textblock text="Binding List"> ?
Please can you show your "List" class? Does the list element has a "DisplayText" property?
Do you have any binding errors in output window?

Probably need to do a TemplateBinding in the following:

XML
<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.
 
Share this answer
 

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:


XML
<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>
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900