Sivlerlight: the best alternative to a simple WPF-like DataTrigger?
-
Hi, I have a ListBox bond to a collection of following objects with string and bool property:
class Item
{
public string Text {get;set;}
public bool IsBold {get;set;}
}The DataTemplate is simple enough:
<DataTemplate>
<TextBlock Text="{Binding Text}" />
</DataTemplate>(could actually use DisplayMemberPath instead, I know) Now, I want those items that have
IsBold == true
to be bold-faced. As in WPF, where you add a simple DataTrigger that setsFontWeight=Bold
on the TextBlock if IsBold is true. Is something like this possible in Silverlight and what is the recommended approach? Thanks, H. -
Hi, I have a ListBox bond to a collection of following objects with string and bool property:
class Item
{
public string Text {get;set;}
public bool IsBold {get;set;}
}The DataTemplate is simple enough:
<DataTemplate>
<TextBlock Text="{Binding Text}" />
</DataTemplate>(could actually use DisplayMemberPath instead, I know) Now, I want those items that have
IsBold == true
to be bold-faced. As in WPF, where you add a simple DataTrigger that setsFontWeight=Bold
on the TextBlock if IsBold is true. Is something like this possible in Silverlight and what is the recommended approach? Thanks, H.You could always bind a
DataTrigger
inside a style to do this, so you would end up with something like this:<TextBlock Text="{Binding Text}">
<TextBlock.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Text}" Value="True">
<Setter Property="FontWeight" Value="Bold" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>Note that I just typed this up in the browser window, so the syntax may need a minor tweak.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
You could always bind a
DataTrigger
inside a style to do this, so you would end up with something like this:<TextBlock Text="{Binding Text}">
<TextBlock.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Text}" Value="True">
<Setter Property="FontWeight" Value="Bold" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>Note that I just typed this up in the browser window, so the syntax may need a minor tweak.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
Would have been cool is Silverlight supported triggers at all, there is no Style.Triggers collection... :( I'll do some digging, but honestly I didn't expect I'll spend so much time on such a simple matter you just take for granted in WPF.
-
Would have been cool is Silverlight supported triggers at all, there is no Style.Triggers collection... :( I'll do some digging, but honestly I didn't expect I'll spend so much time on such a simple matter you just take for granted in WPF.
I know it's an inelegant hack, but you could always use a value converter here.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
I know it's an inelegant hack, but you could always use a value converter here.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
That is what I used for the time being - BooleanToFontWeightConverter, nice:-) - but I am looking for a more systematic approach because I am sure that design requirements will get more complicated than FontWeight=Bold.
-
That is what I used for the time being - BooleanToFontWeightConverter, nice:-) - but I am looking for a more systematic approach because I am sure that design requirements will get more complicated than FontWeight=Bold.
Potentially, you could wrap things into a nice Blend attached behavior. I've done this to get round SL deficiencies in the past.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility